Skip to content

Commit 29922be

Browse files
authored
[0.8] Fix incorrect assertion in BM_VecSimBasics::UpdateAtBlockSize benchmark (#824)
* Fix incorrect assertion in `BM_VecSimBasics::UpdateAtBlockSize` benchmark (#819) * VecSimIndexInterface: introduce indexMetaDataCapacity for testing returns metadata containers capacity * cover for tiered index (cherry picked from commit f24b65a) * Uncomment include for bm_basics_initialize_fp32.h
1 parent 44df9db commit 29922be

File tree

7 files changed

+60
-5
lines changed

7 files changed

+60
-5
lines changed

src/VecSim/algorithms/brute_force/brute_force.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class BruteForceIndex : public VecSimIndexAbstract<DataType, DistType> {
8888
resizeLabelLookup(idToLabelMapping.size());
8989
}
9090

91+
size_t indexMetaDataCapacity() const override { return idToLabelMapping.capacity(); }
92+
9193
size_t getStoredVectorsCount() const {
9294
size_t actual_stored_vec = 0;
9395
for (auto &block : vectorBlocks) {

src/VecSim/algorithms/hnsw/hnsw.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ class HNSWIndex : public VecSimIndexAbstract<DataType, DistType>,
326326

327327
return actual_stored_vec;
328328
}
329+
330+
size_t indexMetaDataCapacity() const override { return idToMetaData.capacity(); }
329331
#endif
330332

331333
protected:

src/VecSim/algorithms/hnsw/hnsw_tiered.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ class TieredHNSWIndex : public VecSimTieredIndex<DataType, DistType> {
233233

234234
#ifdef BUILD_TESTS
235235
void getDataByLabel(labelType label, std::vector<std::vector<DataType>> &vectors_output) const;
236+
size_t indexMetaDataCapacity() const override {
237+
return this->backendIndex->indexMetaDataCapacity() +
238+
this->frontendIndex->indexMetaDataCapacity();
239+
}
236240
#endif
237241
};
238242

src/VecSim/vec_sim_interface.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,5 +264,15 @@ struct VecSimIndexInterface : public VecsimBaseObject {
264264
}
265265
#ifdef BUILD_TESTS
266266
virtual void fitMemory() = 0;
267+
/**
268+
* @brief get the capacity of the meta data containers.
269+
*
270+
* @return The capacity of the meta data containers in number of elements.
271+
* The value returned from this function may differ from the indexCapacity() function. For
272+
* example, in HNSW, the capacity of the meta data containers is the capacity of the labels
273+
* lookup table, while the capacity of the data containers is the capacity of the vectors
274+
* container.
275+
*/
276+
virtual size_t indexMetaDataCapacity() const = 0;
267277
#endif
268278
};

tests/benchmark/bm_vecsim_basics.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,9 @@ void BM_VecSimBasics<index_type_t>::UpdateAtBlockSize(benchmark::State &st) {
310310
// Calculate vectors needed to reach next block boundary
311311
size_t vecs_to_blocksize =
312312
BM_VecSimGeneral::block_size - (initial_index_size % BM_VecSimGeneral::block_size);
313+
size_t initial_index_cap = index->indexMetaDataCapacity();
314+
assert(initial_index_cap == N_VECTORS + vecs_to_blocksize);
315+
313316
assert(vecs_to_blocksize < BM_VecSimGeneral::block_size);
314317
labelType initial_label_count = index->indexLabelCount();
315318
labelType curr_label = initial_label_count;
@@ -334,24 +337,29 @@ void BM_VecSimBasics<index_type_t>::UpdateAtBlockSize(benchmark::State &st) {
334337

335338
// Benchmark loop: repeatedly delete/add same vector to trigger grow-shrink cycles
336339
labelType label_to_update = curr_label - 1;
337-
size_t index_cap = index->indexCapacity();
340+
size_t index_cap = index->indexMetaDataCapacity();
341+
std::cout << "index_cap after adding vectors " << index_cap << std::endl;
342+
assert(index_cap == initial_index_cap + BM_VecSimGeneral::block_size);
343+
338344
for (auto _ : st) {
339345
// Remove the vector directly from hnsw
340346
size_t ret = VecSimIndex_DeleteVector(
341347
INDICES[st.range(0) == VecSimAlgo_TIERED ? VecSimAlgo_HNSWLIB : st.range(0)],
342348
label_to_update);
343349
assert(ret == 1);
344-
assert(index->indexCapacity() == index_cap - BM_VecSimGeneral::block_size);
345-
// Capacity should shrink by one block after deletion
350+
351+
// Capacity should not change
352+
size_t curr_cap = index->indexMetaDataCapacity();
353+
assert(curr_cap == index_cap);
346354
ret = VecSimIndex_AddVector(index, QUERIES[(added_vec_count - 1) % N_QUERIES].data(),
347355
label_to_update);
348356
assert(ret == 1);
349357
BM_VecSimGeneral::mock_thread_pool.thread_pool_wait();
350358
assert(VecSimIndex_IndexSize(
351359
INDICES[st.range(0) == VecSimAlgo_TIERED ? VecSimAlgo_HNSWLIB : st.range(0)]) ==
352360
N_VECTORS + added_vec_count);
353-
// Capacity should grow back to original size after addition
354-
assert(index->indexCapacity() == index_cap);
361+
// Capacity should not change
362+
assert(index->indexMetaDataCapacity() == index_cap);
355363
}
356364
assert(VecSimIndex_IndexSize(index) == N_VECTORS + added_vec_count);
357365

tests/unit/test_allocator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ TYPED_TEST(IndexAllocatorTest, test_bf_index_block_size_1) {
120120

121121
ASSERT_EQ(bfIndex->indexCapacity(), expected_map_containers_size);
122122
ASSERT_EQ(bfIndex->idToLabelMapping.capacity(), expected_map_containers_size);
123+
ASSERT_EQ(bfIndex->indexMetaDataCapacity(), expected_map_containers_size);
123124
ASSERT_EQ(bfIndex->idToLabelMapping.size(), expected_map_containers_size);
124125
ASSERT_GE(bfIndex->labelToIdLookup.bucket_count(), expected_map_containers_size);
125126
};
@@ -530,6 +531,7 @@ TYPED_TEST(IndexAllocatorTest, test_hnsw_reclaim_memory) {
530531
ASSERT_EQ(hnswIndex->getStoredVectorsCount(), expected_size);
531532

532533
ASSERT_EQ(hnswIndex->idToMetaData.capacity(), expected_map_containers_size);
534+
ASSERT_EQ(hnswIndex->indexMetaDataCapacity(), expected_map_containers_size);
533535
ASSERT_EQ(hnswIndex->idToMetaData.size(), expected_map_containers_size);
534536
ASSERT_GE(hnswIndex->labelLookup.bucket_count(), expected_map_containers_size);
535537
// Also validate that there are no unidirectional connections (these add memory to the

tests/unit/test_hnsw_tiered.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3860,6 +3860,11 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
38603860
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
38613861
ASSERT_EQ(hnsw_index->indexSize(), 1);
38623862
ASSERT_EQ(hnsw_index->indexCapacity(), blockSize);
3863+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), blockSize);
3864+
ASSERT_EQ(tiered_index->frontendIndex->indexMetaDataCapacity(), 0);
3865+
ASSERT_EQ(tiered_index->indexMetaDataCapacity(),
3866+
hnsw_index->indexMetaDataCapacity() +
3867+
tiered_index->frontendIndex->indexMetaDataCapacity());
38633868

38643869
// add up to block size
38653870
for (size_t i = 1; i < blockSize; i++) {
@@ -3870,6 +3875,11 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
38703875
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
38713876
ASSERT_EQ(hnsw_index->indexSize(), blockSize);
38723877
ASSERT_EQ(hnsw_index->indexCapacity(), blockSize);
3878+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), blockSize);
3879+
ASSERT_EQ(tiered_index->frontendIndex->indexMetaDataCapacity(), 0);
3880+
ASSERT_EQ(tiered_index->indexMetaDataCapacity(),
3881+
hnsw_index->indexMetaDataCapacity() +
3882+
tiered_index->frontendIndex->indexMetaDataCapacity());
38733883

38743884
// add one more vector to trigger another resize
38753885
GenerateAndAddVector<TEST_DATA_T>(tiered_index, dim, blockSize);
@@ -3879,6 +3889,11 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
38793889
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
38803890
ASSERT_EQ(hnsw_index->indexSize(), blockSize + 1);
38813891
ASSERT_EQ(hnsw_index->indexCapacity(), 2 * blockSize);
3892+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), 2 * blockSize);
3893+
ASSERT_EQ(tiered_index->frontendIndex->indexMetaDataCapacity(), 0);
3894+
ASSERT_EQ(tiered_index->indexMetaDataCapacity(),
3895+
hnsw_index->indexMetaDataCapacity() +
3896+
tiered_index->frontendIndex->indexMetaDataCapacity());
38823897

38833898
// delete a vector to shrink data blocks
38843899
ASSERT_EQ(VecSimIndex_DeleteVector(tiered_index, 0), 1) << "Failed to delete vector 0";
@@ -3890,6 +3905,8 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
38903905
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
38913906
ASSERT_EQ(hnsw_index->indexSize(), blockSize);
38923907
ASSERT_EQ(hnsw_index->indexCapacity(), blockSize);
3908+
// meta data capacity should not shrink
3909+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), 2 * blockSize);
38933910

38943911
// add this vector again and verify lock was acquired to resize
38953912
GenerateAndAddVector<TEST_DATA_T>(tiered_index, dim, 0);
@@ -3898,6 +3915,11 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
38983915
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
38993916
ASSERT_EQ(hnsw_index->indexSize(), blockSize + 1);
39003917
ASSERT_EQ(hnsw_index->indexCapacity(), 2 * blockSize);
3918+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), 2 * blockSize);
3919+
ASSERT_EQ(tiered_index->frontendIndex->indexMetaDataCapacity(), 0);
3920+
ASSERT_EQ(tiered_index->indexMetaDataCapacity(),
3921+
hnsw_index->indexMetaDataCapacity() +
3922+
tiered_index->frontendIndex->indexMetaDataCapacity());
39013923

39023924
// add up to block size (count = 2 blockSize), the lock shouldn't be acquired because no resize
39033925
// is required
@@ -3908,4 +3930,9 @@ TYPED_TEST(HNSWTieredIndexTestBasic, HNSWResize) {
39083930
ASSERT_EQ(tiered_index->getMainIndexGuardWriteLockCount(), resize_operations);
39093931
ASSERT_EQ(hnsw_index->indexSize(), 2 * blockSize);
39103932
ASSERT_EQ(hnsw_index->indexCapacity(), 2 * blockSize);
3933+
ASSERT_EQ(hnsw_index->indexMetaDataCapacity(), 2 * blockSize);
3934+
ASSERT_EQ(tiered_index->frontendIndex->indexMetaDataCapacity(), 0);
3935+
ASSERT_EQ(tiered_index->indexMetaDataCapacity(),
3936+
hnsw_index->indexMetaDataCapacity() +
3937+
tiered_index->frontendIndex->indexMetaDataCapacity());
39113938
}

0 commit comments

Comments
 (0)