From 3cbe959efe379acd839cd3e45874eafb20852a57 Mon Sep 17 00:00:00 2001 From: Dor Forer Date: Tue, 3 Nov 2026 11:55:16 +0200 Subject: [PATCH] Forward is_normalized from the HNSWParams NewIndex overload HNSWFactory::NewIndex(const HNSWParams *, bool is_normalized) dropped is_normalized when forwarding to the VecSimParams overload, so a quantized Cosine index declared as pre-normalized was rejected instead of resolving to IP. Forward the flag and cover both outcomes. Co-Authored-By: Claude Opus 5.5 --- src/VecSim/index_factories/hnsw_factory.cpp | 2 +- tests/unit/test_hnsw_sq8.cpp | 22 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/VecSim/index_factories/hnsw_factory.cpp b/src/VecSim/index_factories/hnsw_factory.cpp index 2e553146f..473c89abd 100644 --- a/src/VecSim/index_factories/hnsw_factory.cpp +++ b/src/VecSim/index_factories/hnsw_factory.cpp @@ -165,7 +165,7 @@ VecSimIndex *NewIndex(const VecSimParams *params, bool is_normalized) { VecSimIndex *NewIndex(const HNSWParams *params, bool is_normalized) { VecSimParams vecSimParams = {.algoParams = {.hnswParams = HNSWParams{*params}}}; - return NewIndex(&vecSimParams); + return NewIndex(&vecSimParams, is_normalized); } template diff --git a/tests/unit/test_hnsw_sq8.cpp b/tests/unit/test_hnsw_sq8.cpp index 10d177d1c..6e3023ed2 100644 --- a/tests/unit/test_hnsw_sq8.cpp +++ b/tests/unit/test_hnsw_sq8.cpp @@ -1131,6 +1131,28 @@ TEST(HNSWSQ8ParamsTest, RejectsOutOfRangeMetric) { EXPECT_EQ(EstimateInitialSize(hnsw_params), SIZE_MAX); } +// The HNSWParams overload of NewIndex must forward is_normalized. A standalone SQ8 index accepts +// Cosine only when the caller declares the input already normalized (it then resolves to IP). +TEST(HNSWSQ8ParamsTest, HNSWParamsOverloadForwardsIsNormalized) { + HNSWParams params = {.type = VecSimType_FLOAT32, + .dim = 16, + .metric = VecSimMetric_Cosine, + .quantType = VecSimQuant_SQ8}; + + // VecSimIndex_Free does not accept NULL, so free only what was created. + VecSimIndex *normalized = HNSWFactory::NewIndex(¶ms, true); + EXPECT_NE(normalized, nullptr); + if (normalized) { + VecSimIndex_Free(normalized); + } + + VecSimIndex *unnormalized = HNSWFactory::NewIndex(¶ms, false); + EXPECT_EQ(unnormalized, nullptr); + if (unnormalized) { + VecSimIndex_Free(unnormalized); + } +} + // Exercise both endpoints, a mean not representable in FP16, and odd dimensions that // also cover SIMD residual handling. TEST(HNSWSQ8ParamsTest, MeanCenteredFP16L2BoundedValues) {