Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/VecSim/algorithms/hnsw/hnsw_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HNSWSerializer : public Serializer {
DEPRECATED = 2, // Last deprecated version
V3,
V4,
V5, // SQ8 quantization fields (quantType + mean vector)
V5, // scalar quantization fields (quantType + mean vector)
INVALID
};

Expand Down
4 changes: 2 additions & 2 deletions src/VecSim/algorithms/hnsw/hnsw_serializer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,10 @@ void HNSWIndex<DataType, DistType>::saveIndexFields(std::ofstream &output) const
writeBinaryPOD(output, this->maxElements); // This will be used to restore the index initial
// capacity

// V5 fields: SQ8 quantization support (between factory params and index build params)
// V5 fields: scalar quantization support (between factory params and index build params)
if (this->quantType != VecSimQuant_NONE) {
writeBinaryPOD(output, this->quantType);
// Write mean vector (dim floats). Empty vector means no mean (zero-mean SQ8).
// Write mean vector (dim floats). Empty vector means no mean (zero-mean quantization).
bool hasMean = !this->serializedMeanVector.empty();
writeBinaryPOD(output, hasMean);
if (hasMean) {
Expand Down
47 changes: 33 additions & 14 deletions src/VecSim/index_factories/components/components_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ template <typename DataType>
return alignment;
}

template <typename DataType, VecSimMetric Metric, bool WithMean>
template <typename DataType, VecSimMetric Metric, bool WithMean, typename StorageType>
IndexComponents<DataType, float>
CreateSQ8IndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size_t dim,
const float *mean_ptr) {
using Preprocessor = QuantPreprocessor<DataType, Metric, WithMean>;
CreateQuantizedIndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size_t dim,
const float *mean_ptr) {
using Preprocessor = QuantPreprocessor<DataType, Metric, WithMean, StorageType>;
using QueryType = typename Preprocessor::QueryType;
unsigned char storage_alignment = 0, asym_storage_alignment = 0;

// Graph construction compares two stored SQ8 blobs; search compares a stored blob with a
// Graph construction compares two stored quantized blobs; search compares a stored blob with a
// QueryType query. Both dispatchers report alignment for the stored operand.
auto sym_func = spaces::GetDistFunc<vecsim_types::sq8, float>(Metric, dim, &storage_alignment);
auto asym_func = spaces::GetDistFunc<vecsim_types::sq8, float, QueryType>(
Metric, dim, &asym_storage_alignment);
auto sym_func = spaces::GetDistFunc<StorageType, float>(Metric, dim, &storage_alignment);
auto asym_func =
spaces::GetDistFunc<StorageType, float, QueryType>(Metric, dim, &asym_storage_alignment);
storage_alignment = spaces::combineAlignments(storage_alignment, asym_storage_alignment);
const unsigned char query_alignment = GetQueryAlignment<QueryType>(Metric, dim);

Expand All @@ -69,7 +69,7 @@ CreateSQ8IndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size
}

pp = new (allocator) Preprocessor(allocator, dim, mean_vec);
calc = new (allocator) DistanceCalculatorWithNorm<DataType, float, Metric>(
calc = new (allocator) DistanceCalculatorWithNorm<DataType, float, Metric, StorageType>(
allocator, asym_func, sym_func, mean_sum_squares);
} else {
pp = new (allocator) Preprocessor(allocator, dim);
Expand All @@ -79,19 +79,38 @@ CreateSQ8IndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size
auto *container = new (allocator)
MultiPreprocessorsContainer<DataType, 1>(allocator, query_alignment, storage_alignment);
[[maybe_unused]] const int ret = container->addPreprocessor(pp);
assert(ret != -1 && "SQ8 preprocessor was not added correctly");
assert(ret != -1 && "Quantization preprocessor was not added correctly");

return {calc, container};
}

// Run-time overload: selects WithMean from whether mean_ptr is set.
template <typename DataType, VecSimMetric Metric, typename StorageType>
IndexComponents<DataType, float>
CreateQuantizedIndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size_t dim,
const float *mean_ptr) {
if (mean_ptr) {
return CreateQuantizedIndexComponents<DataType, Metric, true, StorageType>(allocator, dim,
mean_ptr);
}
return CreateQuantizedIndexComponents<DataType, Metric, false, StorageType>(allocator, dim,
mean_ptr);
}

template <typename DataType, VecSimMetric Metric, bool WithMean>
IndexComponents<DataType, float>
CreateSQ8IndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size_t dim,
const float *mean_ptr) {
return CreateQuantizedIndexComponents<DataType, Metric, WithMean, vecsim_types::sq8>(
allocator, dim, mean_ptr);
}

template <typename DataType, VecSimMetric Metric>
IndexComponents<DataType, float>
CreateSQ8IndexComponents(const std::shared_ptr<VecSimAllocator> &allocator, size_t dim,
const float *mean_ptr) {
if (mean_ptr) {
return CreateSQ8IndexComponents<DataType, Metric, true>(allocator, dim, mean_ptr);
}
return CreateSQ8IndexComponents<DataType, Metric, false>(allocator, dim, mean_ptr);
return CreateQuantizedIndexComponents<DataType, Metric, vecsim_types::sq8>(allocator, dim,
mean_ptr);
}

template <typename DataType, typename DistType>
Expand Down
123 changes: 60 additions & 63 deletions src/VecSim/index_factories/hnsw_factory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,50 @@ NewIndex_ChooseMultiOrSingle(const HNSWParams *params,
HNSWIndex_Single<DataType, DistType>(params, abstractInitParams, components);
}

template <VecSimMetric Metric>
[[nodiscard]] constexpr size_t GetSQ8StoredDataSize(size_t dim, bool with_norm) {
template <VecSimMetric Metric, typename StorageType = sq8>
[[nodiscard]] constexpr size_t GetQuantizedStoredDataSize(size_t dim, bool with_norm) {
static_assert(Metric == VecSimMetric_L2 || Metric == VecSimMetric_IP);

return with_norm ? sq8::storage_bytes_count<Metric, true>(dim)
: sq8::storage_bytes_count<Metric, false>(dim);
return with_norm ? StorageType::template storage_bytes_count<Metric, true>(dim)
: StorageType::template storage_bytes_count<Metric, false>(dim);
}

// Cosine over pre-normalized vectors is computed as inner product.
[[nodiscard]] constexpr VecSimMetric ResolveSQ8Metric(VecSimMetric metric, bool is_normalized) {
[[nodiscard]] constexpr VecSimMetric ResolveQuantizedMetric(VecSimMetric metric,
bool is_normalized) {
return (is_normalized && metric == VecSimMetric_Cosine) ? VecSimMetric_IP : metric;
}

// Keep construction and initial-size validation in sync.
[[nodiscard]] constexpr bool SQ8ParamsSupported(VecSimType type, VecSimMetric resolved_metric) {
[[nodiscard]] constexpr bool QuantizationParamsSupported(VecSimQuantType quant_type,
VecSimType type,
VecSimMetric resolved_metric) {
// Reject unknown quantizers instead of silently creating an unquantized index.
if (quant_type != VecSimQuant_SQ8) {
return false;
}
// SQ8 kernels accept only FLOAT32 and FLOAT16 input.
if (type != VecSimType_FLOAT32 && type != VecSimType_FLOAT16) {
return false;
}
// Only L2 and inner product have SQ8 kernels.
// Only L2 and inner product have scalar quantization kernels.
if (resolved_metric != VecSimMetric_L2 && resolved_metric != VecSimMetric_IP) {
return false;
}
return true;
}

template <typename DataType, VecSimMetric Metric>
VecSimIndex *NewIndex_SQ8(const HNSWParams *hnswParams, AbstractIndexInitParams abstractInitParams,
const float *mean_ptr) {
template <typename DataType, VecSimMetric Metric, typename StorageType = sq8>
VecSimIndex *NewIndex_Quantized(const HNSWParams *hnswParams,
AbstractIndexInitParams abstractInitParams, const float *mean_ptr) {
const bool with_norm = mean_ptr != nullptr;
abstractInitParams.storedDataSize =
GetSQ8StoredDataSize<Metric>(abstractInitParams.dim, with_norm);
GetQuantizedStoredDataSize<Metric, StorageType>(abstractInitParams.dim, with_norm);
abstractInitParams.isQuantized = true;

IndexComponents<DataType, float> components = CreateSQ8IndexComponents<DataType, Metric>(
abstractInitParams.allocator, abstractInitParams.dim, mean_ptr);
IndexComponents<DataType, float> components =
CreateQuantizedIndexComponents<DataType, Metric, StorageType>(
abstractInitParams.allocator, abstractInitParams.dim, mean_ptr);
auto index =
NewIndex_ChooseMultiOrSingle<DataType, float>(hnswParams, abstractInitParams, components);
#ifdef BUILD_TESTS
Expand All @@ -91,38 +99,33 @@ VecSimIndex *NewIndex(const VecSimParams *params, bool is_normalized) {
VecSimFactory::NewAbstractInitParams(hnswParams, params->logCtx, is_normalized);

if (hnswParams->quantType != VecSimQuant_NONE) {
// Reject unknown quantizers instead of silently creating an unquantized index.
if (hnswParams->quantType != VecSimQuant_SQ8) {
return NULL;
}

const VecSimMetric metric = ResolveSQ8Metric(hnswParams->metric, is_normalized);
const VecSimMetric metric = ResolveQuantizedMetric(hnswParams->metric, is_normalized);
const float *mean_ptr = static_cast<const float *>(hnswParams->quantParams);

if (!SQ8ParamsSupported(hnswParams->type, metric)) {
if (!QuantizationParamsSupported(hnswParams->quantType, hnswParams->type, metric)) {
return NULL;
}

if (hnswParams->type == VecSimType_FLOAT32) {
if (metric == VecSimMetric_L2) {
return NewIndex_SQ8<float, VecSimMetric_L2>(hnswParams, abstractInitParams,
mean_ptr);
return NewIndex_Quantized<float, VecSimMetric_L2>(hnswParams, abstractInitParams,
mean_ptr);
} else if (metric == VecSimMetric_IP) {
return NewIndex_SQ8<float, VecSimMetric_IP>(hnswParams, abstractInitParams,
mean_ptr);
return NewIndex_Quantized<float, VecSimMetric_IP>(hnswParams, abstractInitParams,
mean_ptr);
}
} else if (hnswParams->type == VecSimType_FLOAT16) {
if (metric == VecSimMetric_L2) {
return NewIndex_SQ8<float16, VecSimMetric_L2>(hnswParams, abstractInitParams,
mean_ptr);
return NewIndex_Quantized<float16, VecSimMetric_L2>(hnswParams, abstractInitParams,
mean_ptr);
} else if (metric == VecSimMetric_IP) {
return NewIndex_SQ8<float16, VecSimMetric_IP>(hnswParams, abstractInitParams,
mean_ptr);
return NewIndex_Quantized<float16, VecSimMetric_IP>(hnswParams, abstractInitParams,
mean_ptr);
}
}

// Keep the return for release builds so future dispatch changes cannot fall through.
assert(false && "unhandled SQ8 data type and metric combination");
assert(false && "unhandled quantized data type and metric combination");
return NULL;
}

Expand Down Expand Up @@ -189,8 +192,8 @@ size_t EstimateInitialSize(const HNSWParams *params, bool is_normalized, bool wi

if (params->quantType != VecSimQuant_NONE) {
// Keep construction and initial-size validation in sync.
if (params->quantType != VecSimQuant_SQ8 ||
!SQ8ParamsSupported(params->type, ResolveSQ8Metric(params->metric, is_normalized))) {
if (!QuantizationParamsSupported(params->quantType, params->type,
ResolveQuantizedMetric(params->metric, is_normalized))) {
throw std::invalid_argument("Unsupported quantization params for HNSW index");
}
// Template arguments do not affect these component sizes.
Expand Down Expand Up @@ -244,9 +247,9 @@ size_t EstimateElementSize(const HNSWParams *params, bool with_mean) {
// Preserve the element estimator's existing contract: return a size and validate in NewIndex.
if (params->quantType == VecSimQuant_SQ8) {
if (params->metric == VecSimMetric_L2) {
stored_data_size = GetSQ8StoredDataSize<VecSimMetric_L2>(params->dim, with_mean);
stored_data_size = GetQuantizedStoredDataSize<VecSimMetric_L2>(params->dim, with_mean);
} else {
stored_data_size = GetSQ8StoredDataSize<VecSimMetric_IP>(params->dim, with_mean);
stored_data_size = GetQuantizedStoredDataSize<VecSimMetric_IP>(params->dim, with_mean);
}
} else {
stored_data_size =
Expand Down Expand Up @@ -302,6 +305,19 @@ inline VecSimIndex *NewIndex_ChooseMultiOrSingle(std::ifstream &input, const HNS
return index;
}

template <typename DataType, VecSimMetric Metric, typename StorageType = sq8>
VecSimIndex *LoadIndex_Quantized(std::ifstream &input, const HNSWParams &params,
AbstractIndexInitParams abstractInitParams,
HNSWSerializer::EncodingVersion version, const float *mean_ptr) {
abstractInitParams.isQuantized = true;
abstractInitParams.storedDataSize =
GetQuantizedStoredDataSize<Metric, StorageType>(params.dim, mean_ptr != nullptr);
auto components = CreateQuantizedIndexComponents<DataType, Metric, StorageType>(
abstractInitParams.allocator, params.dim, mean_ptr);
return NewIndex_ChooseMultiOrSingle<DataType, float>(input, &params, abstractInitParams,
components, version, mean_ptr);
}

// Initialize @params from file for V3+. For V5+ also reads quantType and mean vector.
// Returns false if the file holds a quantizer value this version does not know.
static bool InitializeParams(std::ifstream &source_params, HNSWParams &params,
Expand All @@ -324,7 +340,7 @@ static bool InitializeParams(std::ifstream &source_params, HNSWParams &params,
return false;
}
params.quantType = static_cast<VecSimQuantType>(raw_quant_type);
if (params.quantType == VecSimQuant_SQ8) {
if (params.quantType != VecSimQuant_NONE) {
bool hasMean = false;
Serializer::readBinaryPOD(source_params, hasMean);
if (hasMean) {
Expand Down Expand Up @@ -370,46 +386,27 @@ VecSimIndex *NewIndex(const std::string &location, bool is_normalized) {

if (params.quantType != VecSimQuant_NONE) {
const float *mean_ptr = meanVector.empty() ? nullptr : meanVector.data();
const VecSimMetric metric = ResolveSQ8Metric(params.metric, is_normalized);
const VecSimMetric metric = ResolveQuantizedMetric(params.metric, is_normalized);

if (!SQ8ParamsSupported(params.type, metric)) {
if (!QuantizationParamsSupported(params.quantType, params.type, metric)) {
return NULL;
}

// Override blob sizes for SQ8 storage layout.
size_t dim = params.dim;
abstractInitParams.isQuantized = true;
if (metric == VecSimMetric_L2) {
abstractInitParams.storedDataSize =
GetSQ8StoredDataSize<VecSimMetric_L2>(dim, mean_ptr != nullptr);
} else {
abstractInitParams.storedDataSize =
GetSQ8StoredDataSize<VecSimMetric_IP>(dim, mean_ptr != nullptr);
}

if (params.type == VecSimType_FLOAT32) {
if (metric == VecSimMetric_L2) {
auto components = CreateSQ8IndexComponents<float, VecSimMetric_L2>(
abstractInitParams.allocator, dim, mean_ptr);
return NewIndex_ChooseMultiOrSingle<float>(input, &params, abstractInitParams,
components, version, mean_ptr);
return LoadIndex_Quantized<float, VecSimMetric_L2>(
input, params, abstractInitParams, version, mean_ptr);
} else {
auto components = CreateSQ8IndexComponents<float, VecSimMetric_IP>(
abstractInitParams.allocator, dim, mean_ptr);
return NewIndex_ChooseMultiOrSingle<float>(input, &params, abstractInitParams,
components, version, mean_ptr);
return LoadIndex_Quantized<float, VecSimMetric_IP>(
input, params, abstractInitParams, version, mean_ptr);
}
} else if (params.type == VecSimType_FLOAT16) {
if (metric == VecSimMetric_L2) {
auto components = CreateSQ8IndexComponents<float16, VecSimMetric_L2>(
abstractInitParams.allocator, dim, mean_ptr);
return NewIndex_ChooseMultiOrSingle<float16, float>(
input, &params, abstractInitParams, components, version, mean_ptr);
return LoadIndex_Quantized<float16, VecSimMetric_L2>(
input, params, abstractInitParams, version, mean_ptr);
} else {
auto components = CreateSQ8IndexComponents<float16, VecSimMetric_IP>(
abstractInitParams.allocator, dim, mean_ptr);
return NewIndex_ChooseMultiOrSingle<float16, float>(
input, &params, abstractInitParams, components, version, mean_ptr);
return LoadIndex_Quantized<float16, VecSimMetric_IP>(
input, params, abstractInitParams, version, mean_ptr);
}
}
}
Expand Down
Loading
Loading