Skip to content

Commit 2f979ed

Browse files
committed
calculate EstimateElementSize accroding to the stored vector size
add tests
1 parent 8b5bcd5 commit 2f979ed

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

cmake/san.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ if (USE_ASAN OR USE_MSAN)
77
set(CMAKE_LINKER "${CMAKE_C_COMPILER}")
88

99
if (USE_ASAN)
10-
set(CLANG_SAN_FLAGS "-fno-omit-frame-pointer -fsanitize=address")
10+
set(CLANG_SAN_FLAGS "-fno-omit-frame-pointer -fsanitize=address -fsized-deallocation")
1111

1212
elseif (USE_MSAN)
1313
set(CLANG_SAN_FLAGS "-fno-omit-frame-pointer -fsanitize=memory -fsanitize-memory-track-origins=2")

src/VecSim/index_factories/brute_force_factory.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,9 @@ size_t EstimateInitialSize(const BFParams *params, bool is_normalized) {
127127
return est;
128128
}
129129

130-
/*** ======================================================= */
131-
// TODO: replace params->dim * VecSimType_sizeof(params->type) with getStoredDataSize!!!
132-
/** ======================================================= */
133-
134130
size_t EstimateElementSize(const BFParams *params) {
135131
// counting the vector size + idToLabel entry + LabelToIds entry (map reservation)
136-
return params->dim * VecSimType_sizeof(params->type) + sizeof(labelType) + sizeof(void *);
132+
return VecSimParams_GetStoredDataSize(params->type, params->dim, params->metric) +
133+
sizeof(labelType) + sizeof(void *);
137134
}
138135
}; // namespace BruteForceFactory

src/VecSim/index_factories/hnsw_factory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ size_t EstimateElementSize(const HNSWParams *params) {
126126
size_t elementGraphDataSize = sizeof(ElementGraphData) + sizeof(idType) * M * 2;
127127

128128
size_t size_total_data_per_element =
129-
elementGraphDataSize + params->dim * VecSimType_sizeof(params->type);
129+
elementGraphDataSize +
130+
VecSimParams_GetStoredDataSize(params->type, params->dim, params->metric);
130131

131132
// when reserving space for new labels in the lookup hash table, each entry is a pointer to a
132133
// label node (bucket).

tests/unit/test_int8.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,24 @@ TEST_F(INT8TieredTest, elementSizeEstimation) {
301301
EXPECT_NO_FATAL_FAILURE(element_size_test(hnsw_params));
302302
}
303303

304+
// ======= Cosine size estimation (stored vectors include additional space for norm)
305+
TEST_F(INT8HNSWTest, elementSizeEstimation_Cosine) {
306+
size_t M = 64;
307+
308+
HNSWParams params = {.dim = 4, .metric = VecSimMetric_Cosine, .M = M};
309+
EXPECT_NO_FATAL_FAILURE(element_size_test(params));
310+
}
311+
312+
TEST_F(INT8BruteForceTest, elementSizeEstimation_Cosine) {
313+
BFParams params = {.dim = 4, .metric = VecSimMetric_Cosine};
314+
EXPECT_NO_FATAL_FAILURE(element_size_test(params));
315+
}
316+
317+
TEST_F(INT8TieredTest, elementSizeEstimation_Cosine) {
318+
size_t M = 64;
319+
HNSWParams hnsw_params = {.dim = 4, .metric = VecSimMetric_Cosine, .M = M};
320+
EXPECT_NO_FATAL_FAILURE(element_size_test(hnsw_params));
321+
}
304322
/* ---------------------------- Functionality tests ---------------------------- */
305323

306324
template <typename params_t>

tests/unit/test_uint8.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,25 @@ TEST_F(UINT8TieredTest, elementSizeEstimation) {
300300
EXPECT_NO_FATAL_FAILURE(element_size_test(hnsw_params));
301301
}
302302

303+
// ======= Cosine size estimation (stored vectors include additional space for norm)
304+
TEST_F(UINT8HNSWTest, elementSizeEstimation_Cosine) {
305+
size_t M = 64;
306+
307+
HNSWParams params = {.dim = 4, .metric = VecSimMetric_Cosine, .M = M};
308+
EXPECT_NO_FATAL_FAILURE(element_size_test(params));
309+
}
310+
311+
TEST_F(UINT8BruteForceTest, elementSizeEstimation_Cosine) {
312+
BFParams params = {.dim = 4, .metric = VecSimMetric_Cosine};
313+
EXPECT_NO_FATAL_FAILURE(element_size_test(params));
314+
}
315+
316+
TEST_F(UINT8TieredTest, elementSizeEstimation_Cosine) {
317+
size_t M = 64;
318+
HNSWParams hnsw_params = {.dim = 4, .metric = VecSimMetric_Cosine, .M = M};
319+
EXPECT_NO_FATAL_FAILURE(element_size_test(hnsw_params));
320+
}
321+
303322
/* ---------------------------- Functionality tests ---------------------------- */
304323

305324
template <typename params_t>

0 commit comments

Comments
 (0)