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
29 changes: 29 additions & 0 deletions src/VecSim/spaces/spaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
#include "VecSim/types/bfloat16.h"
#include "VecSim/types/float16.h"
#include "VecSim/types/sq8.h"
#include "VecSim/spaces/space_includes.h"
#include "VecSim/spaces/spaces.h"
#include "VecSim/spaces/IP_space.h"
Expand Down Expand Up @@ -100,6 +101,34 @@ dist_func_t<float> GetDistFunc<uint8_t, float>(VecSimMetric metric, size_t dim,
throw std::invalid_argument("Invalid metric");
}

template <>
dist_func_t<float> GetDistFunc<vecsim_types::sq8, float>(VecSimMetric metric, size_t dim,
unsigned char *alignment) {
switch (metric) {
case VecSimMetric_Cosine:
return Cosine_SQ8_SQ8_GetDistFunc(dim, alignment);
case VecSimMetric_IP:
return IP_SQ8_SQ8_GetDistFunc(dim, alignment);
case VecSimMetric_L2:
return L2_SQ8_SQ8_GetDistFunc(dim, alignment);
}
throw std::invalid_argument("Invalid metric");
}

template <>
dist_func_t<float> GetDistFunc<vecsim_types::sq8, float, float>(VecSimMetric metric, size_t dim,
unsigned char *alignment) {
switch (metric) {
case VecSimMetric_Cosine:
return Cosine_SQ8_GetDistFunc(dim, alignment);
case VecSimMetric_IP:
return IP_SQ8_GetDistFunc(dim, alignment);
case VecSimMetric_L2:
return L2_SQ8_GetDistFunc(dim, alignment);
}
throw std::invalid_argument("Invalid metric");
}

template <>
normalizeVector_f<float> GetNormalizeFunc<float>(void) {
return normalizeVector_imp<float>;
Expand Down
9 changes: 5 additions & 4 deletions src/VecSim/spaces/spaces.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ namespace spaces {
template <typename RET_TYPE>
using dist_func_t = RET_TYPE (*)(const void *, const void *, size_t);

// Set the distance function for a given data type, metric and dimension. The alignment hint is
// determined according to the chosen implementation and available optimizations.

template <typename DataType, typename DistType>
// Get the distance function for comparing vectors of type VecType1 and VecType2, for a given metric
// and dimension. The returned function has the signature: dist(VecType1*, VecType2*, size_t) ->
// DistType. VecType2 defaults to VecType1 when both vectors are of the same type. The alignment
// hint is set based on the chosen implementation and available optimizations.
template <typename VecType1, typename DistType, typename VecType2 = VecType1>
dist_func_t<DistType> GetDistFunc(VecSimMetric metric, size_t dim, unsigned char *alignment);

template <typename DataType>
Expand Down
22 changes: 22 additions & 0 deletions src/VecSim/types/sq8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 <cstdint>
#include <cstddef>
#include "VecSim/vec_sim_common.h"

namespace vecsim_types {

// Represents a scalar-quantized 8-bit blob with reconstruction metadata
struct sq8 {
using value_type = uint8_t;
};

} // namespace vecsim_types
38 changes: 38 additions & 0 deletions tests/unit/test_spaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "VecSim/spaces/IP_space.h"
#include "VecSim/spaces/L2_space.h"
#include "VecSim/types/float16.h"
#include "VecSim/types/sq8.h"
#include "VecSim/spaces/functions/AVX512F.h"
#include "VecSim/spaces/functions/AVX.h"
#include "VecSim/spaces/functions/SSE.h"
Expand All @@ -43,6 +44,7 @@

using bfloat16 = vecsim_types::bfloat16;
using float16 = vecsim_types::float16;
using sq8 = vecsim_types::sq8;
using namespace spaces;

class SpacesTest : public ::testing::Test {
Expand Down Expand Up @@ -509,6 +511,42 @@ TEST_F(SpacesTest, GetDistFuncInvalidMetricUINT8) {
(spaces::GetDistFunc<uint8_t, float>((VecSimMetric)(VecSimMetric_Cosine + 1), 10, nullptr)),
std::invalid_argument);
}
TEST_F(SpacesTest, GetDistFuncInvalidMetricSQ8) {
// SQ8 to SQ8 (symmetric)
EXPECT_THROW(
(spaces::GetDistFunc<sq8, float>((VecSimMetric)(VecSimMetric_Cosine + 1), 10, nullptr)),
std::invalid_argument);
}
TEST_F(SpacesTest, GetDistFuncInvalidMetricSQ8ToFloat) {
// SQ8 to float (asymmetric)
EXPECT_THROW((spaces::GetDistFunc<sq8, float, float>((VecSimMetric)(VecSimMetric_Cosine + 1),
10, nullptr)),
std::invalid_argument);
}

// Positive tests for GetDistFunc - verify correct function is returned
TEST_F(SpacesTest, GetDistFuncSQ8Symmetric) {
// SQ8 to SQ8 (symmetric) - should return SQ8_SQ8 functions
size_t dim = 128;
auto l2_func = spaces::GetDistFunc<sq8, float>(VecSimMetric_L2, dim, nullptr);
auto ip_func = spaces::GetDistFunc<sq8, float>(VecSimMetric_IP, dim, nullptr);
auto cosine_func = spaces::GetDistFunc<sq8, float>(VecSimMetric_Cosine, dim, nullptr);
ASSERT_EQ(l2_func, L2_SQ8_SQ8_GetDistFunc(dim, nullptr));
ASSERT_EQ(ip_func, IP_SQ8_SQ8_GetDistFunc(dim, nullptr));
ASSERT_EQ(cosine_func, Cosine_SQ8_SQ8_GetDistFunc(dim, nullptr));
}

TEST_F(SpacesTest, GetDistFuncSQ8Asymmetric) {
// SQ8 to float (asymmetric) - should return SQ8 functions
size_t dim = 128;
auto l2_func = spaces::GetDistFunc<sq8, float, float>(VecSimMetric_L2, dim, nullptr);
auto ip_func = spaces::GetDistFunc<sq8, float, float>(VecSimMetric_IP, dim, nullptr);
auto cosine_func = spaces::GetDistFunc<sq8, float, float>(VecSimMetric_Cosine, dim, nullptr);
ASSERT_EQ(l2_func, L2_SQ8_GetDistFunc(dim, nullptr));
ASSERT_EQ(ip_func, IP_SQ8_GetDistFunc(dim, nullptr));
ASSERT_EQ(cosine_func, Cosine_SQ8_GetDistFunc(dim, nullptr));
}

#ifdef CPU_FEATURES_ARCH_X86_64
TEST_F(SpacesTest, smallDimChooser) {
// Verify that small dimensions gets the no optimization function.
Expand Down
Loading