Repository navigation
[MOD-21133] Implement MVP SQ8 for FP64 #1121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3ec8d58
74916ca
c91659c
df6419d
87cbcc5
e277cc7
5999e57
50fb62c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /* | ||
| * Copyright (c) 2006-Present, Redis Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under your choice of the Redis Source Available License 2.0 | ||
| * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| * GNU Affero General Public License v3 (AGPLv3). | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include "VecSim/spaces/computer/preprocessors.h" | ||
| #include "VecSim/types/sq8_fp64.h" | ||
|
|
||
| // Cosine inputs must already be normalized by the caller. | ||
| template <VecSimMetric Metric> | ||
| class SQ8FP64Preprocessor : public PreprocessorInterface { | ||
| static_assert(Metric == VecSimMetric_L2 || Metric == VecSimMetric_IP || | ||
| Metric == VecSimMetric_Cosine); | ||
|
|
||
| public: | ||
| SQ8FP64Preprocessor(std::shared_ptr<VecSimAllocator> allocator, size_t dim); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we extend |
||
| void preprocess(const void *original, void *&storage, void *&query, size_t &storage_size, | ||
| size_t &query_size, unsigned char storage_alignment, | ||
| unsigned char query_alignment) const override; | ||
| void preprocessForStorage(const void *original, void *&storage, size_t &storage_size, | ||
| unsigned char alignment) const override; | ||
| void preprocessQuery(const void *original, void *&query, size_t &query_size, | ||
| unsigned char alignment) const override; | ||
| void preprocessStorageInPlace(void *original, size_t input_size) const override; | ||
|
|
||
| private: | ||
| static uint8_t to_code(double scaled); | ||
| void quantize(const double *input, uint8_t *output) const; | ||
| const size_t dim; | ||
| }; | ||
|
|
||
| template <VecSimMetric Metric> | ||
| SQ8FP64Preprocessor<Metric>::SQ8FP64Preprocessor(std::shared_ptr<VecSimAllocator> allocator, | ||
| size_t dim) | ||
| : PreprocessorInterface(allocator), dim(dim) { | ||
| assert(dim > 0); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| uint8_t SQ8FP64Preprocessor<Metric>::to_code(double scaled) { | ||
| if (!(scaled > 0.0)) { | ||
| return 0; | ||
| } | ||
|
|
||
| if (scaled >= vecsim_types::sq8_fp64::max_code) { | ||
| return vecsim_types::sq8_fp64::max_code; | ||
| } | ||
|
|
||
| return static_cast<uint8_t>(scaled + 0.5); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| void SQ8FP64Preprocessor<Metric>::quantize(const double *input, uint8_t *output) const { | ||
| const auto [min_it, max_it] = std::minmax_element(input, input + dim); | ||
| const double minimum = *min_it; | ||
| const double range = *max_it - minimum; | ||
| const double delta = range == 0.0 ? 1.0 : range / vecsim_types::sq8_fp64::max_code; | ||
| double sum = 0.0; | ||
| double squares = 0.0; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| const uint8_t code = to_code((input[i] - minimum) / delta); | ||
| output[i] = code; | ||
| const double value = minimum + delta * code; | ||
| sum += value; | ||
| if constexpr (Metric == VecSimMetric_L2) { | ||
| squares += value * value; | ||
| } | ||
| } | ||
| const double metadata[] = {minimum, delta, sum, squares}; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The FP64 IP and L2 kernels only consume the codes, minimum, and delta. Do we need the stored/query sums and sums of squares yet? Unless another consumer requires them, could we avoid computing and storing unused metadata? |
||
| std::memcpy(output + vecsim_types::sq8_fp64::data_bytes_count(dim), metadata, | ||
| vecsim_types::sq8_fp64::storage_metadata_count<Metric>() * sizeof(double)); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| void SQ8FP64Preprocessor<Metric>::preprocess(const void *original, void *&storage, void *&query, | ||
| size_t &storage_size, size_t &query_size, | ||
| unsigned char storage_alignment, | ||
| unsigned char query_alignment) const { | ||
| assert(!storage && !query); | ||
| assert(&storage_size != &query_size); | ||
| preprocessForStorage(original, storage, storage_size, storage_alignment); | ||
| preprocessQuery(original, query, query_size, query_alignment); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| void SQ8FP64Preprocessor<Metric>::preprocessForStorage(const void *original, void *&storage, | ||
| size_t &storage_size, | ||
| unsigned char alignment) const { | ||
| assert(!storage); | ||
| storage_size = vecsim_types::sq8_fp64::storage_bytes_count<Metric>(dim); | ||
| storage = this->allocator->allocate_aligned(storage_size, alignment); | ||
| quantize(static_cast<const double *>(original), static_cast<uint8_t *>(storage)); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| void SQ8FP64Preprocessor<Metric>::preprocessQuery(const void *original, void *&query, | ||
| size_t &query_size, | ||
| unsigned char alignment) const { | ||
| assert(!query); | ||
| const auto *const input = static_cast<const double *>(original); | ||
| double sum = 0.0; | ||
| double squares = 0.0; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| sum += input[i]; | ||
| if constexpr (Metric == VecSimMetric_L2) { | ||
| squares += input[i] * input[i]; | ||
| } | ||
| } | ||
| query_size = vecsim_types::sq8_fp64::query_bytes_count<Metric>(dim); | ||
| query = this->allocator->allocate_aligned(query_size, alignment); | ||
| std::memcpy(query, original, dim * sizeof(double)); | ||
| const double metadata[] = {sum, squares}; | ||
| std::memcpy(static_cast<uint8_t *>(query) + dim * sizeof(double), metadata, | ||
| vecsim_types::sq8_fp64::query_metadata_count<Metric>() * sizeof(double)); | ||
| } | ||
|
|
||
| template <VecSimMetric Metric> | ||
| void SQ8FP64Preprocessor<Metric>::preprocessStorageInPlace(void *original, | ||
| size_t input_size) const { | ||
| assert(input_size >= vecsim_types::sq8_fp64::storage_bytes_count<Metric>(dim)); | ||
| quantize(static_cast<const double *>(original), static_cast<uint8_t *>(original)); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright (c) 2006-Present, Redis Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under your choice of the Redis Source Available License 2.0 | ||
| * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| * GNU Affero General Public License v3 (AGPLv3). | ||
| */ | ||
| #include "VecSim/spaces/sq8_fp64.h" | ||
| #include "VecSim/spaces/quantized_kernels.h" | ||
| #include "VecSim/types/sq8_fp64.h" | ||
| #include "VecSim/utils/alignment.h" | ||
|
|
||
| namespace spaces { | ||
| namespace { | ||
| double metadata(const void *blob, size_t dim, size_t index) { | ||
| return load_unaligned<double>(static_cast<const uint8_t *>(blob) + | ||
| vecsim_types::sq8_fp64::data_bytes_count(dim) + | ||
| index * sizeof(double)); | ||
| } | ||
|
|
||
| double storedL2(const void *left, const void *right, size_t dim) { | ||
| const auto *const a = static_cast<const uint8_t *>(left); | ||
| const auto *const b = static_cast<const uint8_t *>(right); | ||
| const double min_a = metadata(left, dim, vecsim_types::sq8_fp64::MIN_VAL); | ||
| const double delta_a = metadata(left, dim, vecsim_types::sq8_fp64::DELTA); | ||
| const double min_b = metadata(right, dim, vecsim_types::sq8_fp64::MIN_VAL); | ||
| const double delta_b = metadata(right, dim, vecsim_types::sq8_fp64::DELTA); | ||
| double result = 0.0; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| const double residual = | ||
| (min_a - min_b) + (delta_a - delta_b) * a[i] + delta_b * (int(a[i]) - int(b[i])); | ||
| result += residual * residual; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| template <bool Asymmetric> | ||
| double innerProduct(const void *left, const void *right, size_t dim) { | ||
| const auto *const a = static_cast<const uint8_t *>(left); | ||
| const auto *const b = static_cast<const uint8_t *>(right); | ||
| const double min_a = metadata(left, dim, vecsim_types::sq8_fp64::MIN_VAL); | ||
| const double delta_a = metadata(left, dim, vecsim_types::sq8_fp64::DELTA); | ||
| const double min_b = Asymmetric ? 0.0 : metadata(right, dim, vecsim_types::sq8_fp64::MIN_VAL); | ||
| const double delta_b = Asymmetric ? 0.0 : metadata(right, dim, vecsim_types::sq8_fp64::DELTA); | ||
| double result = 0.0; | ||
| for (size_t i = 0; i < dim; ++i) { | ||
| const double y = | ||
| Asymmetric ? load_unaligned<double>(b + i * sizeof(double)) : min_b + delta_b * b[i]; | ||
| result += (min_a + delta_a * a[i]) * y; | ||
| } | ||
| return 1.0 - result; | ||
| } | ||
| } // namespace | ||
|
|
||
| double L2_SQ8FP64(const void *left, const void *right, size_t dim) { | ||
| return storedL2(left, right, dim); | ||
| } | ||
| double L2_SQ8FP64_FP64(const void *left, const void *right, size_t dim) { | ||
| return quantized::StoredQueryL2<vecsim_types::sq8_fp64>(left, right, dim); | ||
| } | ||
| double IP_SQ8FP64(const void *left, const void *right, size_t dim) { | ||
| return innerProduct<false>(left, right, dim); | ||
| } | ||
| double IP_SQ8FP64_FP64(const void *left, const void *right, size_t dim) { | ||
| return innerProduct<true>(left, right, dim); | ||
| } | ||
| } // namespace spaces |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright (c) 2006-Present, Redis Ltd. | ||
| * All rights reserved. | ||
| * | ||
| * Licensed under your choice of the Redis Source Available License 2.0 | ||
| * (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the | ||
| * GNU Affero General Public License v3 (AGPLv3). | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
|
|
||
| namespace spaces { | ||
| double L2_SQ8FP64(const void *left, const void *right, size_t dim); | ||
| double L2_SQ8FP64_FP64(const void *left, const void *right, size_t dim); | ||
| double IP_SQ8FP64(const void *left, const void *right, size_t dim); | ||
| double IP_SQ8FP64_FP64(const void *left, const void *right, size_t dim); | ||
| } // namespace spaces |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we extend the existing quantized component factory and preprocessor to support FP64 instead of adding parallel implementations? Most of the setup and preprocessing flow is shared. Precision-specific arithmetic can remain separate where necessary.