refactor: remove hamming metric (#365)
* refactor: remove binary metric * fix: fix windows
This commit is contained in:
@@ -19,7 +19,6 @@
|
||||
#include <random>
|
||||
#include <ailego/container/vector_array.h>
|
||||
#include <ailego/math/euclidean_distance_matrix.h>
|
||||
#include <ailego/math/hamming_distance_matrix.h>
|
||||
#include <ailego/math/inner_product_matrix.h>
|
||||
#include <ailego/math/norm2_matrix.h>
|
||||
#include <ailego/math/normalizer.h>
|
||||
@@ -673,164 +672,6 @@ class NibbleKmeansContext {
|
||||
std::vector<Cluster> clusters_{};
|
||||
};
|
||||
|
||||
/*! Binary K-Means Context
|
||||
*/
|
||||
template <typename T, size_t BATCH_COUNT = 32u>
|
||||
class BinaryKmeansContext {
|
||||
public:
|
||||
//! constexpr variables
|
||||
constexpr static size_t BatchCount = BATCH_COUNT;
|
||||
|
||||
//! Type of values
|
||||
using ValueType = typename std::remove_cv<T>::type;
|
||||
using StoreType = typename std::remove_cv<T>::type;
|
||||
|
||||
// Check supporting type
|
||||
static_assert(std::is_same<ValueType, uint32_t>::value ||
|
||||
std::is_same<ValueType, uint64_t>::value,
|
||||
"ValueType must be uint32_t or uint64_t");
|
||||
|
||||
/*! K-Means Context Cluster
|
||||
*/
|
||||
class Cluster {
|
||||
public:
|
||||
//! Constructor
|
||||
Cluster(size_t dim) : accum_(dim, 0) {}
|
||||
|
||||
//! Constructor
|
||||
Cluster(const Cluster &rhs)
|
||||
: cost_(rhs.cost_), count_(rhs.count_), accum_(rhs.accum_) {}
|
||||
|
||||
//! Constructor
|
||||
Cluster(Cluster &&rhs)
|
||||
: cost_(rhs.cost_), count_(rhs.count_), accum_(std::move(rhs.accum_)) {}
|
||||
|
||||
//! Assignment
|
||||
Cluster &operator=(const Cluster &rhs) {
|
||||
cost_ = rhs.cost_;
|
||||
count_ = rhs.count_;
|
||||
accum_ = rhs.accum_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Assignment
|
||||
Cluster &operator=(Cluster &&rhs) {
|
||||
cost_ = rhs.cost_;
|
||||
count_ = rhs.count_;
|
||||
accum_ = std::move(rhs.accum_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
//! Append a vector
|
||||
void append(const ValueType *vec, size_t dim, float dist) {
|
||||
ailego_check_with(dim == accum_.size(), "Unmatched dimension");
|
||||
|
||||
mutex_.lock();
|
||||
cost_ += dist;
|
||||
count_ += 1;
|
||||
|
||||
const uint8_t *arr = reinterpret_cast<const uint8_t *>(vec);
|
||||
for (size_t i = 0; i != dim; ++i) {
|
||||
if (arr[i >> 3] & (1u << (i & 7))) {
|
||||
accum_[i] += 1;
|
||||
}
|
||||
}
|
||||
mutex_.unlock();
|
||||
}
|
||||
|
||||
//! Retrieve the centroid of vectors
|
||||
void centroid(ValueType *out, size_t dim) const {
|
||||
ailego_check_with(dim == accum_.size(), "Unmatched dimension");
|
||||
|
||||
uint8_t *arr = reinterpret_cast<uint8_t *>(out);
|
||||
size_t half = count_ >> 1;
|
||||
for (size_t i = 0; i != dim; ++i) {
|
||||
if (accum_[i] > half) {
|
||||
arr[i >> 3] |= static_cast<uint8_t>(1 << (i & 0x7));
|
||||
} else {
|
||||
arr[i >> 3] &= ~static_cast<uint8_t>(1 << (i & 0x7));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//! Retrieve squared error
|
||||
double cost(void) const {
|
||||
return cost_;
|
||||
}
|
||||
|
||||
//! Retrieve feature count
|
||||
size_t count(void) const {
|
||||
return count_;
|
||||
}
|
||||
|
||||
private:
|
||||
SpinMutex mutex_{};
|
||||
double cost_{0.0};
|
||||
size_t count_{0u};
|
||||
std::vector<uint32_t> accum_{};
|
||||
};
|
||||
|
||||
//! operator []
|
||||
const Cluster &operator[](size_t i) const {
|
||||
return clusters_[i];
|
||||
}
|
||||
|
||||
//! operator []
|
||||
Cluster &operator[](size_t i) {
|
||||
return clusters_[i];
|
||||
}
|
||||
|
||||
//! Clear the context
|
||||
void clear(void) {
|
||||
clusters_.clear();
|
||||
}
|
||||
|
||||
//! Reset the context
|
||||
void reset(size_t k_value, size_t dim) {
|
||||
clusters_.clear();
|
||||
clusters_.resize(k_value, dim);
|
||||
}
|
||||
|
||||
//! Retrieve context of clusters
|
||||
const std::vector<Cluster> &clusters(void) const {
|
||||
return clusters_;
|
||||
}
|
||||
|
||||
//! Compute the distance between matrix and query (batch)
|
||||
template <size_t N>
|
||||
static void BatchDistance(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out) {
|
||||
HammingDistanceMatrix<ValueType, BatchCount, N>::Compute(m, q, dim, out);
|
||||
}
|
||||
|
||||
//! Compute the distance between matrix and query (single)
|
||||
static void Distance(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out) {
|
||||
HammingDistanceMatrix<ValueType, 1, 1>::Compute(m, q, dim, out);
|
||||
}
|
||||
|
||||
//! Transpose a matrix
|
||||
static void MatrixTranspose(const ValueType *src, size_t dim, T *dst) {
|
||||
MatrixHelper::Transpose<ValueType, BatchCount>(
|
||||
src, (dim >> 3) / sizeof(ValueType), dst);
|
||||
}
|
||||
|
||||
//! Reverse transpose a matrix
|
||||
static void MatrixReverseTranspose(const ValueType *src, size_t dim, T *dst) {
|
||||
MatrixHelper::ReverseTranspose<ValueType, BatchCount>(
|
||||
src, (dim >> 3) / sizeof(ValueType), dst);
|
||||
}
|
||||
|
||||
//! Compute Norm2
|
||||
static void Norm2(ValueType * /*data*/, size_t /*dim*/, float *norm) {
|
||||
*norm = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
//! Members
|
||||
std::vector<Cluster> clusters_{};
|
||||
};
|
||||
|
||||
/*! Numerical InnerProduct K-Means Context
|
||||
*/
|
||||
template <typename T, size_t BATCH_COUNT = 32u>
|
||||
@@ -1195,12 +1036,6 @@ template <typename T, typename TPool,
|
||||
typename TContext = NibbleKmeansContext<T>>
|
||||
using NibbleKmeans = LloydCluster<T, TPool, TContext, NibbleVectorArray<T>>;
|
||||
|
||||
/*! Binary K-Means cluster algorithm
|
||||
*/
|
||||
template <typename T, typename TPool,
|
||||
typename TContext = BinaryKmeansContext<T>>
|
||||
using BinaryKmeans = LloydCluster<T, TPool, TContext, BinaryVectorArray<T>>;
|
||||
|
||||
/*! Numerical K-Means cluster algorithm
|
||||
*/
|
||||
template <typename T, typename TPool,
|
||||
|
||||
@@ -22,32 +22,6 @@ namespace ailego {
|
||||
/*! Distance module
|
||||
*/
|
||||
struct Distance {
|
||||
//! Compute the hamming distance between two vectors (BINARY)
|
||||
static float Hamming(const uint32_t *lhs, const uint32_t *rhs, size_t dim) {
|
||||
float result;
|
||||
HammingDistanceMatrix<uint32_t, 1, 1>::Compute(lhs, rhs, dim, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
//! Compute the hamming distance between two vectors (BINARY)
|
||||
static float Hamming(const uint64_t *lhs, const uint64_t *rhs, size_t dim) {
|
||||
float result;
|
||||
HammingDistanceMatrix<uint64_t, 1, 1>::Compute(lhs, rhs, dim, &result);
|
||||
return result;
|
||||
}
|
||||
|
||||
#else
|
||||
//! Compute the hamming distance between two vectors (BINARY)
|
||||
static float Hamming(const uint64_t *lhs, const uint64_t *rhs, size_t dim) {
|
||||
float result;
|
||||
HammingDistanceMatrix<uint32_t, 1, 1>::Compute(
|
||||
reinterpret_cast<const uint32_t *>(lhs),
|
||||
reinterpret_cast<const uint32_t *>(rhs), dim, &result);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
//! Compute the squared euclidean distance between two vectors (FP32)
|
||||
static float SquaredEuclidean(const float *lhs, const float *rhs,
|
||||
size_t dim) {
|
||||
|
||||
@@ -16,6 +16,5 @@
|
||||
|
||||
#include "cosine_distance_matrix.h"
|
||||
#include "euclidean_distance_matrix.h"
|
||||
#include "hamming_distance_matrix.h"
|
||||
#include "inner_product_matrix.h"
|
||||
#include "mips_euclidean_distance_matrix.h"
|
||||
|
||||
@@ -1,365 +0,0 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "hamming_distance_matrix.h"
|
||||
#include <arrow/util/future.h>
|
||||
#include <zvec/ailego/internal/platform.h>
|
||||
#include "distance_matrix_popcnt.i"
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
#define POPCNT_UINT32_STEP1_SSE HAMMING_UINT32_STEP1_SSE
|
||||
#define POPCNT_UINT32_STEP2_SSE HAMMING_UINT32_STEP2_SSE
|
||||
#define POPCNT_UINT32_STEP3_SSE HAMMING_UINT32_STEP3_SSE
|
||||
#define POPCNT_UINT32_STEP1_AVX HAMMING_UINT32_STEP1_AVX
|
||||
#define POPCNT_UINT32_STEP2_AVX HAMMING_UINT32_STEP2_AVX
|
||||
#define POPCNT_UINT32_STEP3_AVX HAMMING_UINT32_STEP3_AVX
|
||||
#define POPCNT_UINT64_STEP1_AVX HAMMING_UINT64_STEP1_AVX
|
||||
#define POPCNT_UINT64_STEP2_AVX HAMMING_UINT64_STEP2_AVX
|
||||
|
||||
//! Calculate population count (Step 1 SSE)
|
||||
#define HAMMING_UINT32_STEP1_SSE(xmm_m, xmm_q, xmm_sum) \
|
||||
xmm_sum = _mm_add_epi8( \
|
||||
VerticalPopCount_INT8_V128(_mm_xor_si128(xmm_m, xmm_q)), xmm_sum);
|
||||
|
||||
//! Calculate population count (Step 2 SSE)
|
||||
#define HAMMING_UINT32_STEP2_SSE(xmm_m, xmm_q, xmm_sum) \
|
||||
xmm_sum = _mm_add_epi16( \
|
||||
VerticalPopCount_INT16_V128(_mm_xor_si128(xmm_m, xmm_q)), xmm_sum);
|
||||
|
||||
//! Calculate population count (Step 3 SSE)
|
||||
#define HAMMING_UINT32_STEP3_SSE(xmm_m, xmm_q, xmm_sum) \
|
||||
xmm_sum = _mm_add_epi32( \
|
||||
VerticalPopCount_INT32_V128(_mm_xor_si128(xmm_m, xmm_q)), xmm_sum);
|
||||
|
||||
//! Calculate population count (Step 1 AVX)
|
||||
#define HAMMING_UINT32_STEP1_AVX(ymm_m, ymm_q, ymm_sum) \
|
||||
ymm_sum = _mm256_add_epi8( \
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_m, ymm_q)), ymm_sum);
|
||||
|
||||
//! Calculate population count (Step 2 AVX)
|
||||
#define HAMMING_UINT32_STEP2_AVX(ymm_m, ymm_q, ymm_sum) \
|
||||
ymm_sum = _mm256_add_epi16( \
|
||||
VerticalPopCount_INT16_V256(_mm256_xor_si256(ymm_m, ymm_q)), ymm_sum);
|
||||
|
||||
//! Calculate population count (Step 3 AVX)
|
||||
#define HAMMING_UINT32_STEP3_AVX(ymm_m, ymm_q, ymm_sum) \
|
||||
ymm_sum = _mm256_add_epi32( \
|
||||
VerticalPopCount_INT32_V256(_mm256_xor_si256(ymm_m, ymm_q)), ymm_sum);
|
||||
|
||||
//! Calculate population count (Step 1 AVX)
|
||||
#define HAMMING_UINT64_STEP1_AVX(ymm_m, ymm_q, ymm_sum) \
|
||||
ymm_sum = _mm256_add_epi8( \
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_m, ymm_q)), ymm_sum);
|
||||
|
||||
//! Calculate population count (Step 2 AVX)
|
||||
#define HAMMING_UINT64_STEP2_AVX(ymm_m, ymm_q, ymm_sum) \
|
||||
ymm_sum = _mm256_add_epi64( \
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_m, ymm_q)), ymm_sum);
|
||||
|
||||
#if defined(__AVX512VL__) && defined(__AVX512DQ__)
|
||||
#define CONVERT_UINT64_TO_FP32(v, ...) _mm256_cvtepu64_ps(v)
|
||||
#elif defined(__AVX2__)
|
||||
static const __m256i CONVERT_UINT32_MASK_AVX =
|
||||
_mm256_set_epi32(0, 0, 0, 0, 6, 4, 2, 0);
|
||||
|
||||
#define CONVERT_UINT64_TO_FP32(v, ...) \
|
||||
_mm_cvtepi32_ps(_mm256_castsi256_si128( \
|
||||
_mm256_permutevar8x32_epi32(v, CONVERT_UINT32_MASK_AVX)))
|
||||
#endif // __AVX512VL__ && __AVX512DQ__
|
||||
|
||||
#define SQRT_UINT64_TO_FP32(v, ...) _mm_sqrt_ps(CONVERT_UINT64_TO_FP32(v))
|
||||
#define SQRT_UINT32_TO_FP32_SSE(v, ...) _mm_sqrt_ps(_mm_cvtepi32_ps(v))
|
||||
#define SQRT_UINT32_TO_FP32_AVX(v, ...) _mm256_sqrt_ps(_mm256_cvtepi32_ps(v))
|
||||
|
||||
#if defined(__AVX2__)
|
||||
static inline size_t HammingDistanceAVX(const uint32_t *lhs,
|
||||
const uint32_t *rhs, size_t size) {
|
||||
__m256i ymm_sum_0 = _mm256_setzero_si256();
|
||||
__m256i ymm_sum_1 = _mm256_setzero_si256();
|
||||
|
||||
const uint32_t *lhs_0 = lhs + ((size >> 4) << 4);
|
||||
const uint32_t *lhs_1 = (size > 496 ? lhs + 496 : lhs_0);
|
||||
const uint32_t *lhs_2 = lhs + size;
|
||||
|
||||
if (((uintptr_t)lhs & 0x1f) == 0 && ((uintptr_t)rhs & 0x1f) == 0) {
|
||||
for (; lhs != lhs_1; lhs += 16, rhs += 16) {
|
||||
__m256i ymm_lhs_0 = _mm256_load_si256((__m256i *)(lhs + 0));
|
||||
__m256i ymm_lhs_1 = _mm256_load_si256((__m256i *)(lhs + 8));
|
||||
__m256i ymm_rhs_0 = _mm256_load_si256((__m256i *)(rhs + 0));
|
||||
__m256i ymm_rhs_1 = _mm256_load_si256((__m256i *)(rhs + 8));
|
||||
|
||||
ymm_sum_0 = _mm256_add_epi8(
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_lhs_0, ymm_rhs_0)),
|
||||
ymm_sum_0);
|
||||
ymm_sum_1 = _mm256_add_epi8(
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_lhs_1, ymm_rhs_1)),
|
||||
ymm_sum_1);
|
||||
}
|
||||
ymm_sum_0 = _mm256_sad_epu8(ymm_sum_0, POPCNT_ZERO_AVX);
|
||||
ymm_sum_1 = _mm256_sad_epu8(ymm_sum_1, POPCNT_ZERO_AVX);
|
||||
|
||||
for (; lhs != lhs_0; lhs += 16, rhs += 16) {
|
||||
__m256i ymm_lhs_0 = _mm256_load_si256((__m256i *)(lhs + 0));
|
||||
__m256i ymm_lhs_1 = _mm256_load_si256((__m256i *)(lhs + 8));
|
||||
__m256i ymm_rhs_0 = _mm256_load_si256((__m256i *)(rhs + 0));
|
||||
__m256i ymm_rhs_1 = _mm256_load_si256((__m256i *)(rhs + 8));
|
||||
|
||||
ymm_sum_0 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs_0, ymm_rhs_0)),
|
||||
ymm_sum_0);
|
||||
ymm_sum_1 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs_1, ymm_rhs_1)),
|
||||
ymm_sum_1);
|
||||
}
|
||||
|
||||
if (lhs_2 >= lhs + 8) {
|
||||
__m256i ymm_lhs = _mm256_load_si256((__m256i *)(lhs));
|
||||
__m256i ymm_rhs = _mm256_load_si256((__m256i *)(rhs));
|
||||
ymm_sum_0 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs, ymm_rhs)),
|
||||
ymm_sum_0);
|
||||
lhs += 8;
|
||||
rhs += 8;
|
||||
}
|
||||
} else {
|
||||
for (; lhs != lhs_1; lhs += 16, rhs += 16) {
|
||||
__m256i ymm_lhs_0 = _mm256_loadu_si256((__m256i *)(lhs + 0));
|
||||
__m256i ymm_lhs_1 = _mm256_loadu_si256((__m256i *)(lhs + 8));
|
||||
__m256i ymm_rhs_0 = _mm256_loadu_si256((__m256i *)(rhs + 0));
|
||||
__m256i ymm_rhs_1 = _mm256_loadu_si256((__m256i *)(rhs + 8));
|
||||
|
||||
ymm_sum_0 = _mm256_add_epi8(
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_lhs_0, ymm_rhs_0)),
|
||||
ymm_sum_0);
|
||||
ymm_sum_1 = _mm256_add_epi8(
|
||||
VerticalPopCount_INT8_V256(_mm256_xor_si256(ymm_lhs_1, ymm_rhs_1)),
|
||||
ymm_sum_1);
|
||||
}
|
||||
ymm_sum_0 = _mm256_sad_epu8(ymm_sum_0, POPCNT_ZERO_AVX);
|
||||
ymm_sum_1 = _mm256_sad_epu8(ymm_sum_1, POPCNT_ZERO_AVX);
|
||||
|
||||
for (; lhs != lhs_0; lhs += 16, rhs += 16) {
|
||||
__m256i ymm_lhs_0 = _mm256_loadu_si256((__m256i *)(lhs + 0));
|
||||
__m256i ymm_lhs_1 = _mm256_loadu_si256((__m256i *)(lhs + 8));
|
||||
__m256i ymm_rhs_0 = _mm256_loadu_si256((__m256i *)(rhs + 0));
|
||||
__m256i ymm_rhs_1 = _mm256_loadu_si256((__m256i *)(rhs + 8));
|
||||
|
||||
ymm_sum_0 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs_0, ymm_rhs_0)),
|
||||
ymm_sum_0);
|
||||
ymm_sum_1 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs_1, ymm_rhs_1)),
|
||||
ymm_sum_1);
|
||||
}
|
||||
|
||||
if (lhs_2 >= lhs + 8) {
|
||||
__m256i ymm_lhs = _mm256_loadu_si256((__m256i *)(lhs));
|
||||
__m256i ymm_rhs = _mm256_loadu_si256((__m256i *)(rhs));
|
||||
ymm_sum_0 = _mm256_add_epi64(
|
||||
VerticalPopCount_INT64_V256(_mm256_xor_si256(ymm_lhs, ymm_rhs)),
|
||||
ymm_sum_0);
|
||||
lhs += 8;
|
||||
rhs += 8;
|
||||
}
|
||||
}
|
||||
|
||||
size_t count =
|
||||
(size_t)HorizontalAdd_INT64_V256(_mm256_add_epi64(ymm_sum_0, ymm_sum_1));
|
||||
switch (lhs_2 - lhs) {
|
||||
case 7:
|
||||
count += ailego_popcount32(lhs[6] ^ rhs[6]);
|
||||
/* FALLTHRU */
|
||||
case 6:
|
||||
count += ailego_popcount32(lhs[5] ^ rhs[5]);
|
||||
/* FALLTHRU */
|
||||
case 5:
|
||||
count += ailego_popcount32(lhs[4] ^ rhs[4]);
|
||||
/* FALLTHRU */
|
||||
case 4:
|
||||
count += ailego_popcount32(lhs[3] ^ rhs[3]);
|
||||
/* FALLTHRU */
|
||||
case 3:
|
||||
count += ailego_popcount32(lhs[2] ^ rhs[2]);
|
||||
/* FALLTHRU */
|
||||
case 2:
|
||||
count += ailego_popcount32(lhs[1] ^ rhs[1]);
|
||||
/* FALLTHRU */
|
||||
case 1:
|
||||
count += ailego_popcount32(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static inline size_t HammingDistanceAVX(const uint64_t *lhs,
|
||||
const uint64_t *rhs, size_t size) {
|
||||
return HammingDistanceAVX(reinterpret_cast<const uint32_t *>(lhs),
|
||||
reinterpret_cast<const uint32_t *>(rhs),
|
||||
(size << 1));
|
||||
}
|
||||
#endif // __AVX2__
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
static inline size_t HammingDistance(const uint32_t *lhs, const uint32_t *rhs,
|
||||
size_t size) {
|
||||
const uint32_t *last = lhs + size;
|
||||
const uint32_t *last_aligned = lhs + ((size >> 3) << 3);
|
||||
size_t count = 0;
|
||||
|
||||
for (; lhs != last_aligned; lhs += 8, rhs += 8) {
|
||||
count += ailego_popcount64(*(uint64_t *)(&lhs[6]) ^ *(uint64_t *)(&rhs[6]));
|
||||
count += ailego_popcount64(*(uint64_t *)(&lhs[4]) ^ *(uint64_t *)(&rhs[4]));
|
||||
count += ailego_popcount64(*(uint64_t *)(&lhs[2]) ^ *(uint64_t *)(&rhs[2]));
|
||||
count += ailego_popcount64(*(uint64_t *)(&lhs[0]) ^ *(uint64_t *)(&rhs[0]));
|
||||
}
|
||||
switch (last - last_aligned) {
|
||||
case 7:
|
||||
count += ailego_popcount32(lhs[6] ^ rhs[6]);
|
||||
/* FALLTHRU */
|
||||
case 6:
|
||||
count += ailego_popcount32(lhs[5] ^ rhs[5]);
|
||||
/* FALLTHRU */
|
||||
case 5:
|
||||
count += ailego_popcount32(lhs[4] ^ rhs[4]);
|
||||
/* FALLTHRU */
|
||||
case 4:
|
||||
count += ailego_popcount32(lhs[3] ^ rhs[3]);
|
||||
/* FALLTHRU */
|
||||
case 3:
|
||||
count += ailego_popcount32(lhs[2] ^ rhs[2]);
|
||||
/* FALLTHRU */
|
||||
case 2:
|
||||
count += ailego_popcount32(lhs[1] ^ rhs[1]);
|
||||
/* FALLTHRU */
|
||||
case 1:
|
||||
count += ailego_popcount32(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
static inline size_t HammingDistance(const uint64_t *lhs, const uint64_t *rhs,
|
||||
size_t size) {
|
||||
const uint64_t *last = lhs + size;
|
||||
const uint64_t *last_aligned = lhs + ((size >> 2) << 2);
|
||||
size_t count = 0;
|
||||
|
||||
for (; lhs != last_aligned; lhs += 4, rhs += 4) {
|
||||
count += ailego_popcount64(lhs[3] ^ rhs[3]);
|
||||
count += ailego_popcount64(lhs[2] ^ rhs[2]);
|
||||
count += ailego_popcount64(lhs[1] ^ rhs[1]);
|
||||
count += ailego_popcount64(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
switch (last - last_aligned) {
|
||||
case 3:
|
||||
count += ailego_popcount64(lhs[2] ^ rhs[2]);
|
||||
/* FALLTHRU */
|
||||
case 2:
|
||||
count += ailego_popcount64(lhs[1] ^ rhs[1]);
|
||||
/* FALLTHRU */
|
||||
case 1:
|
||||
count += ailego_popcount64(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#else
|
||||
static inline size_t HammingDistance(const uint32_t *lhs, const uint32_t *rhs,
|
||||
size_t size) {
|
||||
const uint32_t *last = lhs + size;
|
||||
const uint32_t *last_aligned = lhs + ((size >> 2) << 2);
|
||||
size_t count = 0;
|
||||
|
||||
for (; lhs != last_aligned; lhs += 4, rhs += 4) {
|
||||
count += ailego_popcount32(lhs[3] ^ rhs[3]);
|
||||
count += ailego_popcount32(lhs[2] ^ rhs[2]);
|
||||
count += ailego_popcount32(lhs[1] ^ rhs[1]);
|
||||
count += ailego_popcount32(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
switch (last - last_aligned) {
|
||||
case 3:
|
||||
count += ailego_popcount32(lhs[2] ^ rhs[2]);
|
||||
/* FALLTHRU */
|
||||
case 2:
|
||||
count += ailego_popcount32(lhs[1] ^ rhs[1]);
|
||||
/* FALLTHRU */
|
||||
case 1:
|
||||
count += ailego_popcount32(lhs[0] ^ rhs[0]);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
#endif // AILEGO_M64
|
||||
|
||||
//! Compute the distance between matrix and query (UINT32, M=1, N=1)
|
||||
void HammingDistanceMatrix<uint32_t, 1, 1>::Compute(const ValueType *m,
|
||||
const ValueType *q,
|
||||
size_t dim, float *out) {
|
||||
size_t cnt = (dim >> 5);
|
||||
#if defined(__AVX2__)
|
||||
if (cnt > 63) {
|
||||
*out = static_cast<float>(HammingDistanceAVX(m, q, cnt));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
*out = static_cast<float>(HammingDistance(m, q, cnt));
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
//! Compute the distance between matrix and query (UINT64, M=1, N=1)
|
||||
void HammingDistanceMatrix<uint64_t, 1, 1>::Compute(const ValueType *m,
|
||||
const ValueType *q,
|
||||
size_t dim, float *out) {
|
||||
size_t cnt = (dim >> 6);
|
||||
#if defined(__AVX2__)
|
||||
if (cnt > 31) {
|
||||
*out = static_cast<float>(HammingDistanceAVX(m, q, cnt));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
*out = static_cast<float>(HammingDistance(m, q, cnt));
|
||||
}
|
||||
|
||||
#endif // AILEGO_M64
|
||||
|
||||
//! Compute the distance between matrix and query (UINT32, M=1, N=1)
|
||||
void HammingSquareRootDistanceMatrix<uint32_t, 1, 1>::Compute(
|
||||
const ValueType *m, const ValueType *q, size_t dim, float *out) {
|
||||
size_t cnt = (dim >> 5);
|
||||
#if defined(__AVX2__)
|
||||
if (cnt > 63) {
|
||||
*out = std::sqrt(static_cast<float>(HammingDistanceAVX(m, q, cnt)));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
*out = std::sqrt(static_cast<float>(HammingDistance(m, q, cnt)));
|
||||
}
|
||||
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
//! Compute the distance between matrix and query (UINT64, M=1, N=1)
|
||||
void HammingSquareRootDistanceMatrix<uint64_t, 1, 1>::Compute(
|
||||
const ValueType *m, const ValueType *q, size_t dim, float *out) {
|
||||
size_t cnt = (dim >> 6);
|
||||
#if defined(__AVX2__)
|
||||
if (cnt > 31) {
|
||||
*out = std::sqrt(static_cast<float>(HammingDistanceAVX(m, q, cnt)));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
*out = std::sqrt(static_cast<float>(HammingDistance(m, q, cnt)));
|
||||
}
|
||||
|
||||
#endif // AILEGO_M64
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
@@ -1,193 +0,0 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <zvec/ailego/internal/platform.h>
|
||||
#include <zvec/ailego/utility/type_helper.h>
|
||||
|
||||
namespace zvec {
|
||||
namespace ailego {
|
||||
|
||||
/*! Hamming Distance Matrix
|
||||
*/
|
||||
template <typename T, size_t M, size_t N,
|
||||
typename = void> // NOTE: useless 'typename=void' to avoid clang
|
||||
// compile error
|
||||
struct HammingDistanceMatrix;
|
||||
|
||||
/*! Hamming Distance Matrix (UINT32)
|
||||
*/
|
||||
template <size_t M, size_t N>
|
||||
struct HammingDistanceMatrix<uint32_t, M, N> {
|
||||
//! Type of value
|
||||
using ValueType = uint32_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static inline void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out) {
|
||||
ailego_assert(m && q && !(dim & 31) && out);
|
||||
|
||||
size_t cnt = (dim >> 5);
|
||||
if (cnt > 0) {
|
||||
for (size_t i = 0; i < M; ++i) {
|
||||
ValueType m_val = m[i];
|
||||
float *r = out + i;
|
||||
|
||||
for (size_t j = 0; j < N; ++j) {
|
||||
*r = static_cast<float>(ailego_popcount32(m_val ^ q[j]));
|
||||
r += M;
|
||||
}
|
||||
}
|
||||
m += M;
|
||||
q += N;
|
||||
}
|
||||
|
||||
for (size_t k = 1; k < cnt; ++k) {
|
||||
for (size_t i = 0; i < M; ++i) {
|
||||
ValueType m_val = m[i];
|
||||
float *r = out + i;
|
||||
|
||||
for (size_t j = 0; j < N; ++j) {
|
||||
*r += static_cast<float>(ailego_popcount32(m_val ^ q[j]));
|
||||
r += M;
|
||||
}
|
||||
}
|
||||
m += M;
|
||||
q += N;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*! Hamming Distance Matrix (UINT32, M=1, N=1)
|
||||
*/
|
||||
template <>
|
||||
struct HammingDistanceMatrix<uint32_t, 1, 1> {
|
||||
//! Type of value
|
||||
using ValueType = uint32_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out);
|
||||
};
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
/*! Hamming Distance Matrix (UINT64)
|
||||
*/
|
||||
template <size_t M, size_t N>
|
||||
struct HammingDistanceMatrix<uint64_t, M, N> {
|
||||
//! Type of value
|
||||
using ValueType = uint64_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static inline void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out) {
|
||||
ailego_assert(m && q && !(dim & 63) && out);
|
||||
|
||||
size_t cnt = (dim >> 6);
|
||||
if (cnt > 0) {
|
||||
for (size_t i = 0; i < M; ++i) {
|
||||
ValueType m_val = m[i];
|
||||
float *r = out + i;
|
||||
|
||||
for (size_t j = 0; j < N; ++j) {
|
||||
*r = static_cast<float>(ailego_popcount64(m_val ^ q[j]));
|
||||
r += M;
|
||||
}
|
||||
}
|
||||
m += M;
|
||||
q += N;
|
||||
}
|
||||
|
||||
for (size_t k = 1; k < cnt; ++k) {
|
||||
for (size_t i = 0; i < M; ++i) {
|
||||
ValueType m_val = m[i];
|
||||
float *r = out + i;
|
||||
|
||||
for (size_t j = 0; j < N; ++j) {
|
||||
*r += static_cast<float>(ailego_popcount64(m_val ^ q[j]));
|
||||
r += M;
|
||||
}
|
||||
}
|
||||
m += M;
|
||||
q += N;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*! Hamming Distance Matrix (UINT64, M=1, N=1)
|
||||
*/
|
||||
template <>
|
||||
struct HammingDistanceMatrix<uint64_t, 1, 1> {
|
||||
//! Type of value
|
||||
using ValueType = uint64_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out);
|
||||
};
|
||||
|
||||
#endif // AILEGO_M64
|
||||
|
||||
/*! Hamming Square Root Distance Matrix
|
||||
*/
|
||||
template <typename T, size_t M, size_t N>
|
||||
struct HammingSquareRootDistanceMatrix {
|
||||
//! Type of value
|
||||
using ValueType = typename std::remove_cv<T>::type;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static inline void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out) {
|
||||
ailego_assert(m && q && dim && out);
|
||||
|
||||
HammingDistanceMatrix<T, M, N>::Compute(m, q, dim, out);
|
||||
for (size_t i = 0; i < N * M; ++i) {
|
||||
float val = *out;
|
||||
*out++ = std::sqrt(val);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/*! Hamming Square Root Distance Matrix (UINT32, M=1, N=1)
|
||||
*/
|
||||
template <>
|
||||
struct HammingSquareRootDistanceMatrix<uint32_t, 1, 1> {
|
||||
//! Type of value
|
||||
using ValueType = uint32_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out);
|
||||
};
|
||||
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
/*! Hamming Square Root Distance Matrix (UINT64, M=1, N=1)
|
||||
*/
|
||||
template <>
|
||||
struct HammingSquareRootDistanceMatrix<uint64_t, 1, 1> {
|
||||
//! Type of value
|
||||
using ValueType = uint64_t;
|
||||
|
||||
//! Compute the distance between matrix and query
|
||||
static void Compute(const ValueType *m, const ValueType *q, size_t dim,
|
||||
float *out);
|
||||
};
|
||||
|
||||
#endif // AILEGO_M64
|
||||
|
||||
} // namespace ailego
|
||||
} // namespace zvec
|
||||
@@ -393,16 +393,6 @@ int OptKmeansAlgorithm::mount(IndexFeatures::Pointer feats) {
|
||||
return IndexError_Mismatch;
|
||||
}
|
||||
break;
|
||||
case IndexMeta::DataType::DT_BINARY32:
|
||||
case IndexMeta::DataType::DT_BINARY64:
|
||||
if (feats->dimension() % 32 != 0) {
|
||||
LOG_ERROR(
|
||||
"Unsupported feature dimension %zu (dimension of binary "
|
||||
"must be an integer multiple of 32).",
|
||||
feats->dimension());
|
||||
return IndexError_Mismatch;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -759,137 +749,6 @@ int NibbleKmeansAlgorithm<T>::cluster(IndexThreads::Pointer threads,
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Binary K-Means cluster algorithm
|
||||
*/
|
||||
template <typename T>
|
||||
class BinaryKmeansAlgorithm : public OptKmeansAlgorithm {
|
||||
public:
|
||||
//! Type of value
|
||||
using ValueType = typename std::remove_cv<T>::type;
|
||||
|
||||
// Check supporting type
|
||||
static_assert(ailego::IsArithmetic<ValueType>::value,
|
||||
"ValueType must be arithmetic");
|
||||
|
||||
//! Constructor
|
||||
BinaryKmeansAlgorithm(void) {}
|
||||
|
||||
//! Destructor
|
||||
virtual ~BinaryKmeansAlgorithm(void) {}
|
||||
|
||||
//! Cluster
|
||||
virtual int cluster(IndexThreads::Pointer threads,
|
||||
IndexCluster::CentroidList ¢s);
|
||||
|
||||
protected:
|
||||
//! update centroids
|
||||
void update_centroids(IndexCluster::CentroidList ¢s,
|
||||
const ailego::BinaryKmeans<T, IndexThreads> &algorithm);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void BinaryKmeansAlgorithm<T>::update_centroids(
|
||||
IndexCluster::CentroidList ¢s,
|
||||
const ailego::BinaryKmeans<T, IndexThreads> &algorithm) {
|
||||
this->init_centroids(algorithm.centroids().count(), ¢s);
|
||||
for (size_t i = 0; i < cents.size(); ++i) {
|
||||
IndexCluster::Centroid *centroid = &(cents.at(i));
|
||||
centroid->set_score(algorithm.context().clusters()[i].cost());
|
||||
centroid->set_follows(algorithm.context().clusters()[i].count());
|
||||
centroid->set_feature(algorithm.centroids()[i], (meta_.dimension() >> 3));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
int BinaryKmeansAlgorithm<T>::cluster(IndexThreads::Pointer threads,
|
||||
IndexCluster::CentroidList ¢s) {
|
||||
ailego::ElapsedTime stamp;
|
||||
|
||||
if (!threads) {
|
||||
threads = std::make_shared<SingleQueueIndexThreads>(thread_count_, false);
|
||||
if (!threads) {
|
||||
return IndexError_NoMemory;
|
||||
}
|
||||
}
|
||||
if (!this->check_centroids(cents)) {
|
||||
LOG_ERROR("The input centroid's list includes some invalid centroids.");
|
||||
return IndexError_InvalidArgument;
|
||||
}
|
||||
|
||||
if (!this->is_valid()) {
|
||||
LOG_ERROR("The cluster is not ready.");
|
||||
return IndexError_NoReady;
|
||||
}
|
||||
|
||||
// get cluster algorithm
|
||||
size_t centroid_count =
|
||||
cents.empty()
|
||||
? std::min(cluster_count_, static_cast<uint32_t>(features_->count()))
|
||||
: cents.size();
|
||||
if (centroid_count == 0) {
|
||||
LOG_ERROR("The count of cluster is unknown.");
|
||||
return IndexError_NoReady;
|
||||
}
|
||||
ailego::BinaryKmeans<T, IndexThreads> algorithm(centroid_count,
|
||||
meta_.dimension());
|
||||
|
||||
// mount features into algorithm
|
||||
auto features_count = features_->count();
|
||||
auto dim = meta_.dimension();
|
||||
for (size_t i = 0; i < features_count; ++i) {
|
||||
auto vec = reinterpret_cast<const T *>(features_->element(i));
|
||||
algorithm.append(vec, dim);
|
||||
}
|
||||
|
||||
if (!cents.empty()) {
|
||||
auto centroids = algorithm.mutable_centroids();
|
||||
centroids->reserve(cents.size());
|
||||
for (const auto &it : cents) {
|
||||
centroids->append(reinterpret_cast<const T *>(it.feature()),
|
||||
meta_.dimension());
|
||||
}
|
||||
} else {
|
||||
ailego::Kmc2CentroidsGenerator<
|
||||
ailego::BinaryKmeans<ValueType, IndexThreads>, IndexThreads>
|
||||
g;
|
||||
g.set_chain_length(markov_chain_length_);
|
||||
g.set_assumption_free(assumption_free_);
|
||||
algorithm.init_centroids(*threads, g);
|
||||
}
|
||||
|
||||
double cost = 0.0;
|
||||
|
||||
for (uint32_t i = 0; i < max_iterations_; ++i) {
|
||||
double old_cost, new_epsilon;
|
||||
old_cost = cost;
|
||||
|
||||
bool result = algorithm.cluster_once(*threads, &cost);
|
||||
if (result != true) {
|
||||
LOG_ERROR("(%u) Failed to cluster.", i + 1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
new_epsilon = std::abs(cost - old_cost);
|
||||
LOG_DEBUG(
|
||||
"(%u) Updated %zu Clusters, %zu Features: %zu ms, %f -> "
|
||||
"%f = %f",
|
||||
i, algorithm.centroids().count(), features_->count(),
|
||||
(size_t)stamp.milli_seconds(), old_cost, cost, new_epsilon);
|
||||
stamp.reset();
|
||||
|
||||
if (new_epsilon < epsilon_) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// update centroids
|
||||
update_centroids(cents, algorithm);
|
||||
|
||||
// Purge the empty centroids
|
||||
PurgeCentroids(cents, purge_empty_);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Numerical K-Means cluster algorithm
|
||||
*/
|
||||
template <typename T>
|
||||
@@ -1252,7 +1111,7 @@ int OptKmeansCluster::init(const IndexMeta &meta,
|
||||
const ailego::Params ¶ms) {
|
||||
auto type_ = meta.data_type();
|
||||
|
||||
if (meta.metric_name() == "InnerProduct" || meta.metric_name() == "Cosine") {
|
||||
if (meta.metric_name() == "InnerProduct") {
|
||||
switch (type_) {
|
||||
case IndexMeta::DataType::DT_FP16: {
|
||||
algorithm_.reset(
|
||||
@@ -1317,19 +1176,6 @@ int OptKmeansCluster::init(const IndexMeta &meta,
|
||||
algorithm_.reset(new (std::nothrow) NibbleKmeansAlgorithm<int32_t>);
|
||||
break;
|
||||
}
|
||||
// TODO
|
||||
case IndexMeta::DataType::DT_BINARY32: {
|
||||
algorithm_.reset(new (std::nothrow) BinaryKmeansAlgorithm<uint32_t>);
|
||||
break;
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
case IndexMeta::DataType::DT_BINARY64: {
|
||||
algorithm_.reset(new (std::nothrow) BinaryKmeansAlgorithm<uint64_t>);
|
||||
break;
|
||||
}
|
||||
#endif // AILEGO_M64
|
||||
|
||||
default: {
|
||||
LOG_ERROR("Unsupported feature types %d.", type_);
|
||||
return IndexError_Mismatch;
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <ailego/math/euclidean_distance_matrix.h>
|
||||
#include <ailego/math/hamming_distance_matrix.h>
|
||||
#include <ailego/math_batch/distance_batch.h>
|
||||
#include <zvec/core/framework/index_error.h>
|
||||
#include <zvec/core/framework/index_factory.h>
|
||||
@@ -504,240 +503,6 @@ static inline IndexMetric::MatrixDistanceHandle EuclideanDistanceMatrixInt4(
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
|
||||
//! Retrieve distance function for index features
|
||||
static inline IndexMetric::MatrixDistanceHandle HammingDistanceMatrix32(
|
||||
size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
static inline IndexMetric::MatrixDistanceHandle HammingDistanceMatrix64(
|
||||
size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
#endif // AILEGO_M64
|
||||
|
||||
//! Retrieve distance function for index features
|
||||
static inline IndexMetric::MatrixDistanceHandle
|
||||
HammingSquareRootDistanceMatrix32(size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
static inline IndexMetric::MatrixDistanceHandle
|
||||
HammingSquareRootDistanceMatrix64(size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
#endif // AILEGO_M64
|
||||
|
||||
/*! Squared Euclidean Distance Metric
|
||||
*/
|
||||
class SquaredEuclideanMetric : public IndexMetric {
|
||||
@@ -748,9 +513,7 @@ class SquaredEuclideanMetric : public IndexMetric {
|
||||
if (dt != IndexMeta::DataType::DT_FP16 &&
|
||||
dt != IndexMeta::DataType::DT_FP32 &&
|
||||
dt != IndexMeta::DataType::DT_INT8 &&
|
||||
dt != IndexMeta::DataType::DT_INT4 &&
|
||||
dt != IndexMeta::DataType::DT_BINARY32 &&
|
||||
dt != IndexMeta::DataType::DT_BINARY64) {
|
||||
dt != IndexMeta::DataType::DT_INT4) {
|
||||
return IndexError_Unsupported;
|
||||
}
|
||||
if (IndexMeta::UnitSizeof(dt) != meta.unit_size()) {
|
||||
@@ -784,16 +547,6 @@ class SquaredEuclideanMetric : public IndexMetric {
|
||||
//! Retrieve distance function for query
|
||||
MatrixDistance distance(void) const override {
|
||||
switch (data_type_) {
|
||||
case IndexMeta::DataType::DT_BINARY32:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 1, 1>::Compute);
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
case IndexMeta::DataType::DT_BINARY64:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 1, 1>::Compute);
|
||||
#endif // AILEGO_M64
|
||||
|
||||
case IndexMeta::DataType::DT_FP16:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::SquaredEuclideanDistanceMatrix<ailego::Float16, 1,
|
||||
@@ -825,14 +578,6 @@ class SquaredEuclideanMetric : public IndexMetric {
|
||||
//! Retrieve distance function for index features
|
||||
MatrixDistance distance_matrix(size_t m, size_t n) const override {
|
||||
switch (data_type_) {
|
||||
case IndexMeta::DataType::DT_BINARY32:
|
||||
return HammingDistanceMatrix32(m, n);
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
case IndexMeta::DataType::DT_BINARY64:
|
||||
return HammingDistanceMatrix64(m, n);
|
||||
#endif // AILEGO_M64
|
||||
|
||||
case IndexMeta::DataType::DT_FP16:
|
||||
return SquaredEuclideanDistanceMatrixFp16(m, n);
|
||||
|
||||
@@ -903,9 +648,7 @@ class EuclideanMetric : public IndexMetric {
|
||||
if (dt != IndexMeta::DataType::DT_FP16 &&
|
||||
dt != IndexMeta::DataType::DT_FP32 &&
|
||||
dt != IndexMeta::DataType::DT_INT8 &&
|
||||
dt != IndexMeta::DataType::DT_INT4 &&
|
||||
dt != IndexMeta::DataType::DT_BINARY32 &&
|
||||
dt != IndexMeta::DataType::DT_BINARY64) {
|
||||
dt != IndexMeta::DataType::DT_INT4) {
|
||||
return IndexError_Unsupported;
|
||||
}
|
||||
if (IndexMeta::UnitSizeof(dt) != meta.unit_size()) {
|
||||
@@ -938,16 +681,6 @@ class EuclideanMetric : public IndexMetric {
|
||||
//! Retrieve distance function for query
|
||||
MatrixDistance distance(void) const override {
|
||||
switch (data_type_) {
|
||||
case IndexMeta::DataType::DT_BINARY32:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint32_t, 1, 1>::Compute);
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
case IndexMeta::DataType::DT_BINARY64:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingSquareRootDistanceMatrix<uint64_t, 1, 1>::Compute);
|
||||
#endif // AILEGO_M64
|
||||
|
||||
case IndexMeta::DataType::DT_FP16:
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::EuclideanDistanceMatrix<ailego::Float16, 1, 1>::Compute);
|
||||
@@ -972,14 +705,6 @@ class EuclideanMetric : public IndexMetric {
|
||||
//! Retrieve distance function for index features
|
||||
MatrixDistance distance_matrix(size_t m, size_t n) const override {
|
||||
switch (data_type_) {
|
||||
case IndexMeta::DataType::DT_BINARY32:
|
||||
return HammingSquareRootDistanceMatrix32(m, n);
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
case IndexMeta::DataType::DT_BINARY64:
|
||||
return HammingSquareRootDistanceMatrix64(m, n);
|
||||
#endif // AILEGO_M64
|
||||
|
||||
case IndexMeta::DataType::DT_FP16:
|
||||
return EuclideanDistanceMatrixFp16(m, n);
|
||||
|
||||
|
||||
@@ -1,223 +0,0 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <ailego/math/hamming_distance_matrix.h>
|
||||
#include <zvec/core/framework/index_error.h>
|
||||
#include <zvec/core/framework/index_factory.h>
|
||||
#include "ailego/math_batch/distance_batch.h"
|
||||
|
||||
namespace zvec {
|
||||
namespace core {
|
||||
|
||||
//! Retrieve distance function for index features
|
||||
static inline IndexMetric::MatrixDistanceHandle HammingDistanceMatrix32(
|
||||
size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
static inline IndexMetric::MatrixDistanceHandle HammingDistanceMatrix64(
|
||||
size_t m, size_t n) {
|
||||
static const IndexMetric::MatrixDistanceHandle distance_table[6][6] = {
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 1, 1>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 2, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 2, 2>::Compute),
|
||||
nullptr, nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 4, 4>::Compute),
|
||||
nullptr, nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 8, 8>::Compute),
|
||||
nullptr, nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 16, 16>::Compute),
|
||||
nullptr},
|
||||
{reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 1>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 2>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 4>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 8>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 16>::Compute),
|
||||
reinterpret_cast<IndexMetric::MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 32, 32>::Compute)},
|
||||
};
|
||||
if (m > 32 || n > 32 || ailego_popcount(m) != 1 || ailego_popcount(n) != 1) {
|
||||
return nullptr;
|
||||
}
|
||||
return distance_table[ailego_ctz(m)][ailego_ctz(n)];
|
||||
}
|
||||
#endif // AILEOG_M64
|
||||
|
||||
/*! Hamming Metric
|
||||
*/
|
||||
class HammingMetric : public IndexMetric {
|
||||
public:
|
||||
//! Initialize Metric
|
||||
int init(const IndexMeta &meta, const ailego::Params &index_params) override {
|
||||
if (meta.data_type() != IndexMeta::DataType::DT_BINARY32 &&
|
||||
meta.data_type() != IndexMeta::DataType::DT_BINARY64) {
|
||||
return IndexError_Unsupported;
|
||||
}
|
||||
if (IndexMeta::UnitSizeof(meta.data_type()) != meta.unit_size()) {
|
||||
return IndexError_Unsupported;
|
||||
}
|
||||
feature_type_ = meta.data_type();
|
||||
params_ = index_params;
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! Cleanup Metric
|
||||
int cleanup(void) override {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! Retrieve if it matched
|
||||
bool is_matched(const IndexMeta &meta) const override {
|
||||
return (meta.data_type() == feature_type_ &&
|
||||
meta.unit_size() == IndexMeta::UnitSizeof(feature_type_));
|
||||
}
|
||||
|
||||
//! Retrieve if it matched
|
||||
bool is_matched(const IndexMeta &meta,
|
||||
const IndexQueryMeta &qmeta) const override {
|
||||
return (qmeta.data_type() == feature_type_ &&
|
||||
qmeta.unit_size() == IndexMeta::UnitSizeof(feature_type_) &&
|
||||
qmeta.dimension() == meta.dimension());
|
||||
}
|
||||
|
||||
//! Retrieve distance function for query
|
||||
MatrixDistance distance(void) const override {
|
||||
#if defined(AILEGO_M64)
|
||||
if (feature_type_ == IndexMeta::DataType::DT_BINARY64) {
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint64_t, 1, 1>::Compute);
|
||||
}
|
||||
#endif
|
||||
if (feature_type_ == IndexMeta::DataType::DT_BINARY32) {
|
||||
return reinterpret_cast<MatrixDistanceHandle>(
|
||||
ailego::HammingDistanceMatrix<uint32_t, 1, 1>::Compute);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//! Retrieve distance function for index features
|
||||
MatrixDistance distance_matrix(size_t m, size_t n) const override {
|
||||
#if defined(AILEGO_M64)
|
||||
if (feature_type_ == IndexMeta::DataType::DT_BINARY64) {
|
||||
return HammingDistanceMatrix64(m, n);
|
||||
}
|
||||
#endif
|
||||
if (feature_type_ == IndexMeta::DataType::DT_BINARY32) {
|
||||
return HammingDistanceMatrix32(m, n);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
//! Retrieve params of Metric
|
||||
const ailego::Params ¶ms(void) const override {
|
||||
return params_;
|
||||
}
|
||||
|
||||
//! Retrieve query metric object of this index metric
|
||||
Pointer query_metric(void) const override {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
IndexMeta::DataType feature_type_{IndexMeta::DataType::DT_BINARY32};
|
||||
ailego::Params params_{};
|
||||
};
|
||||
|
||||
INDEX_FACTORY_REGISTER_METRIC_ALIAS(Hamming, HammingMetric);
|
||||
|
||||
} // namespace core
|
||||
} // namespace zvec
|
||||
@@ -159,7 +159,7 @@ class BinaryConverter : public IndexConverter {
|
||||
size_t dim =
|
||||
ailego::BinaryQuantizer::EncodedSizeInBinary32(dimension_) * 32u;
|
||||
|
||||
meta_.set_metric("Hamming", 0, ailego::Params());
|
||||
meta_.set_metric("SquaredEuclidean", 0, ailego::Params());
|
||||
meta_.set_converter("BinaryConverter", 0, params);
|
||||
meta_.set_meta(IndexMeta::DataType::DT_BINARY32, dim);
|
||||
|
||||
|
||||
@@ -125,80 +125,6 @@ TEST(NumericalKmeans, INT8_General) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NumericalKmeans, BINARY32_General) {
|
||||
const size_t DIMENSION = 16 * 32;
|
||||
const size_t K_VALUE = 20;
|
||||
const size_t COUNT = 20000u;
|
||||
|
||||
ailego::BinaryKmeans<uint32_t, ailego::ThreadPool> kmeans;
|
||||
kmeans.reset(K_VALUE, DIMENSION);
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> dist(0.0, 1.0);
|
||||
|
||||
for (size_t i = 0; i < COUNT; ++i) {
|
||||
ailego::BinaryVector<uint32_t> vec(DIMENSION);
|
||||
for (size_t j = 0; j < DIMENSION; ++j) {
|
||||
if (dist(gen) >= 0.5) {
|
||||
vec.set(j);
|
||||
}
|
||||
}
|
||||
kmeans.append(vec.data(), vec.dimension());
|
||||
}
|
||||
|
||||
ailego::ThreadPool pool;
|
||||
double prev_sse = 0.0;
|
||||
for (size_t i = 0; i < 20; ++i) {
|
||||
double sse = 0.0;
|
||||
EXPECT_TRUE(kmeans.cluster_once(pool, &sse));
|
||||
printf("(%zu) SSE: %f -> %f = %f\n", i, prev_sse, sse, sse - prev_sse);
|
||||
prev_sse = sse;
|
||||
}
|
||||
|
||||
for (auto &it : kmeans.context().clusters()) {
|
||||
printf("%f: %zu\n", it.cost(), it.count());
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(AILEGO_M64)
|
||||
TEST(NumericalKmeans, BINARY64_General) {
|
||||
const size_t DIMENSION = 8 * 64;
|
||||
const size_t K_VALUE = 20;
|
||||
const size_t COUNT = 20000u;
|
||||
|
||||
ailego::BinaryKmeans<uint64_t, ailego::ThreadPool> kmeans;
|
||||
kmeans.reset(K_VALUE, DIMENSION);
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> dist(0.0, 1.0);
|
||||
|
||||
for (size_t i = 0; i < COUNT; ++i) {
|
||||
ailego::BinaryVector<uint64_t> vec(DIMENSION);
|
||||
for (size_t j = 0; j < DIMENSION; ++j) {
|
||||
if (dist(gen) >= 0.5) {
|
||||
vec.set(j);
|
||||
}
|
||||
}
|
||||
kmeans.append(vec.data(), vec.dimension());
|
||||
}
|
||||
|
||||
ailego::ThreadPool pool;
|
||||
double prev_sse = 0.0;
|
||||
for (size_t i = 0; i < 20; ++i) {
|
||||
double sse = 0.0;
|
||||
EXPECT_TRUE(kmeans.cluster_once(pool, &sse));
|
||||
printf("(%zu) SSE: %f -> %f = %f\n", i, prev_sse, sse, sse - prev_sse);
|
||||
prev_sse = sse;
|
||||
}
|
||||
|
||||
for (auto &it : kmeans.context().clusters()) {
|
||||
printf("%f: %zu\n", it.cost(), it.count());
|
||||
}
|
||||
}
|
||||
#endif // AILEGO_M64
|
||||
|
||||
TEST(NibbleKmeans, INT4_General) {
|
||||
const size_t DIMENSION = 32;
|
||||
const size_t K_VALUE = 63;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -17,7 +17,6 @@
|
||||
#include <ailego/algorithm/kmeans.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <zvec/ailego/container/params.h>
|
||||
#include <zvec/ailego/internal/platform.h>
|
||||
#include "zvec/core/framework/index_framework.h"
|
||||
|
||||
using namespace zvec::core;
|
||||
@@ -209,106 +208,6 @@ TEST(OptKmeansCluster, General) {
|
||||
// }
|
||||
// }
|
||||
|
||||
TEST(OptKmeansCluster, BinaryGeneral) {
|
||||
// Prepare index data
|
||||
const uint32_t count = 5000u;
|
||||
const uint32_t dimension = 1024u;
|
||||
|
||||
IndexMeta index_meta;
|
||||
index_meta.set_meta(IndexMeta::DataType::DT_BINARY32, dimension);
|
||||
index_meta.set_metric("SquaredEuclidean", 0, Params());
|
||||
|
||||
std::shared_ptr<CompactIndexFeatures> features(
|
||||
new CompactIndexFeatures(index_meta));
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_real_distribution<float> dist(0.0, 1.0);
|
||||
|
||||
for (uint32_t i = 0; i < count; ++i) {
|
||||
BinaryVector<uint32_t> vec(dimension);
|
||||
for (size_t j = 0; j < dimension; ++j) {
|
||||
if (dist(gen) >= 0.5) {
|
||||
vec.set(j);
|
||||
}
|
||||
}
|
||||
features->emplace(vec.data());
|
||||
}
|
||||
|
||||
std::cout << "---------- FIRST ----------\n";
|
||||
|
||||
// Create a Kmeans cluster
|
||||
IndexCluster::Pointer cluster =
|
||||
IndexFactory::CreateCluster("OptKmeansCluster");
|
||||
ASSERT_TRUE(!!cluster);
|
||||
|
||||
Params params;
|
||||
params.set("proxima.general.cluster.count", 1);
|
||||
params.set("proxima.optkmeans.cluster.count", 56);
|
||||
|
||||
ASSERT_EQ(0, cluster->init(index_meta, params));
|
||||
ASSERT_EQ(0, cluster->mount(features));
|
||||
cluster->suggest(64u);
|
||||
|
||||
auto threads = std::make_shared<SingleQueueIndexThreads>();
|
||||
|
||||
std::cout << "---------- FIRST ----------\n";
|
||||
std::vector<IndexCluster::Centroid> centroids;
|
||||
std::vector<uint32_t> labels;
|
||||
ASSERT_NE(0, cluster->classify(threads, centroids));
|
||||
ASSERT_NE(0, cluster->label(threads, centroids, &labels));
|
||||
ASSERT_EQ(0, cluster->cluster(threads, centroids));
|
||||
|
||||
for (const auto &it : centroids) {
|
||||
const auto &vec = it.vector<uint32_t>();
|
||||
|
||||
unsigned int mask = 0x1;
|
||||
std::cout << it.follows() << " (" << it.score() << ") { "
|
||||
<< !!(vec[0] & mask) << ", " << !!(vec[0] & (mask << 1)) << ", "
|
||||
<< !!(vec[0] & (mask << 2)) << ", ... , "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 2))) << ", "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 1))) << " }"
|
||||
<< std::endl;
|
||||
ASSERT_EQ(0u, it.similars().size());
|
||||
}
|
||||
|
||||
std::cout << "---------- SECOND ----------\n";
|
||||
ASSERT_EQ(0, cluster->cluster(threads, centroids));
|
||||
|
||||
for (const auto &it : centroids) {
|
||||
const auto &vec = it.vector<uint32_t>();
|
||||
|
||||
unsigned int mask = 0x1;
|
||||
std::cout << it.follows() << " (" << it.score() << ") { "
|
||||
<< !!(vec[0] & mask) << ", " << !!(vec[0] & (mask << 1)) << ", "
|
||||
<< !!(vec[0] & (mask << 2)) << ", ... , "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 2))) << ", "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 1))) << " }"
|
||||
<< std::endl;
|
||||
ASSERT_EQ(0u, it.similars().size());
|
||||
}
|
||||
|
||||
std::cout << "---------- THIRD ----------\n";
|
||||
ASSERT_EQ(0, cluster->cluster(threads, centroids));
|
||||
|
||||
for (const auto &it : centroids) {
|
||||
const auto &vec = it.vector<uint32_t>();
|
||||
|
||||
unsigned int mask = 0x1;
|
||||
std::cout << it.follows() << " (" << it.score() << ") { "
|
||||
<< !!(vec[0] & mask) << ", " << !!(vec[0] & (mask << 1)) << ", "
|
||||
<< !!(vec[0] & (mask << 2)) << ", ... , "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 2))) << ", "
|
||||
<< !!(vec[0] & (mask << !!(sizeof(uint32_t) - 1))) << " }"
|
||||
<< std::endl;
|
||||
ASSERT_EQ(0u, it.similars().size());
|
||||
}
|
||||
|
||||
ASSERT_EQ(0, cluster->classify(threads, centroids));
|
||||
ASSERT_EQ(0, cluster->label(threads, centroids, &labels));
|
||||
}
|
||||
|
||||
|
||||
TEST(OptKmeansCluster, IN4General) {
|
||||
// Prepare index data
|
||||
const uint32_t count = 5000u;
|
||||
|
||||
@@ -118,18 +118,6 @@ TEST_F(FlatBuilderTest, TestInt8WithRandomDimension) {
|
||||
ASSERT_EQ(0, builder->init(meta_, params));
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBinaryInvalidColumnMajor) {
|
||||
size_t dim = (DIMENSION + 31) / 32 * 32;
|
||||
meta_.set_metric("Hamming", 0, Params());
|
||||
meta_.set_meta(IndexMeta::DT_BINARY32, dim + 2);
|
||||
meta_.set_major_order(IndexMeta::MO_COLUMN);
|
||||
IndexBuilder::Pointer builder = IndexFactory::CreateBuilder("FlatBuilder");
|
||||
ASSERT_NE(builder, nullptr);
|
||||
Params params;
|
||||
ASSERT_EQ(0, builder->init(meta_, params));
|
||||
std::string path = dir_ + "TestGeneral";
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBuildWithRowMajor) {
|
||||
meta_.set_metric("SquaredEuclidean", 0, Params());
|
||||
meta_.set_major_order(IndexMeta::MO_ROW);
|
||||
@@ -185,35 +173,6 @@ TEST_F(FlatBuilderTest, TestInt8BuildWithRowMajor) {
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBinaryBuildWithRowMajor) {
|
||||
size_t dim = (DIMENSION + 31) / 32 * 32;
|
||||
meta_.set_metric("Hamming", 0, Params());
|
||||
meta_.set_meta(IndexMeta::DT_BINARY32, dim);
|
||||
meta_.set_major_order(IndexMeta::MO_ROW);
|
||||
IndexBuilder::Pointer builder = IndexFactory::CreateBuilder("FlatBuilder");
|
||||
ASSERT_NE(builder, nullptr);
|
||||
Params params;
|
||||
ASSERT_EQ(0, builder->init(meta_, params));
|
||||
std::string path = dir_ + "TestGeneral";
|
||||
|
||||
auto holder =
|
||||
std::make_shared<OnePassIndexHolder<IndexMeta::DT_BINARY32>>(dim);
|
||||
size_t doc_cnt = 128UL;
|
||||
for (size_t i = 0; i < doc_cnt; i++) {
|
||||
BinaryVector<uint32_t> vec(dim);
|
||||
for (size_t j = 0; j < dim && j < i; ++j) {
|
||||
vec.set(j);
|
||||
}
|
||||
ASSERT_TRUE(holder->emplace(i, vec));
|
||||
}
|
||||
|
||||
int ret = builder->train(holder);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = builder->build(holder);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBuildWithColumnMajor) {
|
||||
meta_.set_meta(IndexMeta::DataType::DT_FP32, DIMENSION);
|
||||
meta_.set_metric("SquaredEuclidean", 0, Params());
|
||||
@@ -270,35 +229,6 @@ TEST_F(FlatBuilderTest, TestInt8BuildWithColumnMajor) {
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBinaryBuildWithColumnMajor) {
|
||||
size_t dim = (DIMENSION + 31) / 32 * 32;
|
||||
meta_.set_metric("Hamming", 0, Params());
|
||||
meta_.set_meta(IndexMeta::DT_BINARY32, dim);
|
||||
meta_.set_major_order(IndexMeta::MO_COLUMN);
|
||||
IndexBuilder::Pointer builder = IndexFactory::CreateBuilder("FlatBuilder");
|
||||
ASSERT_NE(builder, nullptr);
|
||||
Params params;
|
||||
ASSERT_EQ(0, builder->init(meta_, params));
|
||||
std::string path = dir_ + "TestGeneral";
|
||||
|
||||
auto holder =
|
||||
std::make_shared<OnePassIndexHolder<IndexMeta::DT_BINARY32>>(dim);
|
||||
size_t doc_cnt = 128UL;
|
||||
for (size_t i = 0; i < doc_cnt; i++) {
|
||||
BinaryVector<uint32_t> vec(dim);
|
||||
for (size_t j = 0; j < dim && j < i; ++j) {
|
||||
vec.set(j);
|
||||
}
|
||||
ASSERT_TRUE(holder->emplace(i, vec));
|
||||
}
|
||||
|
||||
int ret = builder->train(holder);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = builder->build(holder);
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestWithRowMajor) {
|
||||
meta_.set_meta(IndexMeta::DataType::DT_FP32, DIMENSION);
|
||||
meta_.set_metric("SquaredEuclidean", 0, Params());
|
||||
@@ -349,32 +279,6 @@ TEST_F(FlatBuilderTest, TestInt8WithRowMajor) {
|
||||
ASSERT_EQ(0, builder->cleanup());
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBinaryWithRowMajor) {
|
||||
size_t dim = (DIMENSION + 31) / 32 * 32;
|
||||
meta_.set_metric("Hamming", 0, Params());
|
||||
meta_.set_meta(IndexMeta::DT_BINARY32, dim);
|
||||
meta_.set_major_order(IndexMeta::MO_ROW);
|
||||
IndexBuilder::Pointer builder = IndexFactory::CreateBuilder("FlatBuilder");
|
||||
ASSERT_NE(builder, nullptr);
|
||||
Params params;
|
||||
std::string path = dir_ + "TestGeneral";
|
||||
|
||||
auto holder =
|
||||
std::make_shared<OnePassIndexHolder<IndexMeta::DT_BINARY32>>(dim);
|
||||
size_t doc_cnt = 128UL;
|
||||
for (size_t i = 0; i < doc_cnt; i++) {
|
||||
BinaryVector<uint32_t> vec(dim);
|
||||
for (size_t j = 0; j < dim && j < i; ++j) {
|
||||
vec.set(j);
|
||||
}
|
||||
ASSERT_TRUE(holder->emplace(i, vec));
|
||||
}
|
||||
build_process(builder, holder);
|
||||
|
||||
// cleanup and rebuild
|
||||
ASSERT_EQ(0, builder->cleanup());
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestWithColumnMajor) {
|
||||
meta_.set_meta(IndexMeta::DataType::DT_FP32, DIMENSION);
|
||||
meta_.set_metric("SquaredEuclidean", 0, Params());
|
||||
@@ -425,32 +329,6 @@ TEST_F(FlatBuilderTest, TestInt8WithColumnMajor) {
|
||||
ASSERT_EQ(0, builder->cleanup());
|
||||
}
|
||||
|
||||
TEST_F(FlatBuilderTest, TestBinaryWithColumnMajor) {
|
||||
size_t dim = (DIMENSION + 31) / 32 * 32;
|
||||
meta_.set_metric("Hamming", 0, Params());
|
||||
meta_.set_meta(IndexMeta::DT_BINARY32, dim);
|
||||
meta_.set_major_order(IndexMeta::MO_COLUMN);
|
||||
IndexBuilder::Pointer builder = IndexFactory::CreateBuilder("FlatBuilder");
|
||||
ASSERT_NE(builder, nullptr);
|
||||
Params params;
|
||||
std::string path = dir_ + "TestGeneral";
|
||||
|
||||
auto holder =
|
||||
std::make_shared<OnePassIndexHolder<IndexMeta::DT_BINARY32>>(dim);
|
||||
size_t doc_cnt = 128UL;
|
||||
for (size_t i = 0; i < doc_cnt; i++) {
|
||||
BinaryVector<uint32_t> vec(dim);
|
||||
for (size_t j = 0; j < dim && j < i; ++j) {
|
||||
vec.set(j);
|
||||
}
|
||||
ASSERT_TRUE(holder->emplace(i, vec));
|
||||
}
|
||||
build_process(builder, holder);
|
||||
|
||||
// cleanup and rebuild
|
||||
ASSERT_EQ(0, builder->cleanup());
|
||||
}
|
||||
|
||||
#if defined(__GNUC__) || defined(__GNUG__)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
@@ -3447,93 +3447,6 @@ TEST_F(HnswStreamerTest, TestGroupInBruteforceSearch) {
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST_F(HnswStreamerTest, TestBinaryConverter) {
|
||||
uint32_t dimension = 2560;
|
||||
|
||||
IndexStreamer::Pointer streamer =
|
||||
IndexFactory::CreateStreamer("HnswStreamer");
|
||||
ASSERT_TRUE(streamer != nullptr);
|
||||
|
||||
ailego::Params params;
|
||||
// params.set(PARAM_HNSW_STREAMER_MAX_NEIGHBOR_COUNT, 10);
|
||||
// params.set(PARAM_HNSW_STREAMER_SCALING_FACTOR, 16);
|
||||
// params.set(PARAM_HNSW_STREAMER_EFCONSTRUCTION, 10);
|
||||
// params.set(PARAM_HNSW_STREAMER_EF, 5);
|
||||
params.set(PARAM_HNSW_STREAMER_BRUTE_FORCE_THRESHOLD, 1000U);
|
||||
|
||||
ailego::Params stg_params;
|
||||
|
||||
IndexMeta index_meta_raw(IndexMeta::DataType::DT_FP32, dimension);
|
||||
index_meta_raw.set_metric("InnerProduct", 0, ailego::Params());
|
||||
|
||||
ailego::Params converter_params;
|
||||
auto converter = IndexFactory::CreateConverter("BinaryConverter");
|
||||
ASSERT_TRUE(converter != nullptr);
|
||||
|
||||
converter->init(index_meta_raw, converter_params);
|
||||
|
||||
IndexMeta index_meta = converter->meta();
|
||||
|
||||
auto reformer = IndexFactory::CreateReformer(index_meta.reformer_name());
|
||||
ASSERT_TRUE(reformer != nullptr);
|
||||
|
||||
ASSERT_EQ(0, reformer->init(index_meta.reformer_params()));
|
||||
|
||||
auto storage = IndexFactory::CreateStorage("MMapFileStorage");
|
||||
ASSERT_EQ(0, storage->init(stg_params));
|
||||
ASSERT_EQ(0, storage->open(dir_ + "TestBinaryConverter.index", true));
|
||||
ASSERT_EQ(0, streamer->init(index_meta, params));
|
||||
ASSERT_EQ(0, streamer->open(storage));
|
||||
|
||||
size_t cnt = 5000U;
|
||||
auto ctx = streamer->create_context();
|
||||
ASSERT_TRUE(!!ctx);
|
||||
|
||||
IndexQueryMeta qmeta(IndexMeta::DataType::DT_FP32, dimension);
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
std::uniform_real_distribution<float> dist(-2.0, 2.0);
|
||||
std::vector<NumericalVector<float>> vecs;
|
||||
|
||||
for (size_t i = 0; i < cnt; i++) {
|
||||
NumericalVector<float> vec(dimension);
|
||||
for (size_t j = 0; j < dimension; ++j) {
|
||||
vec[j] = dist(gen);
|
||||
}
|
||||
|
||||
std::string new_vec;
|
||||
IndexQueryMeta new_meta;
|
||||
|
||||
ASSERT_EQ(0, reformer->convert(vec.data(), qmeta, &new_vec, &new_meta));
|
||||
ASSERT_EQ(0, streamer->add_impl(i, new_vec.data(), new_meta, ctx));
|
||||
|
||||
vecs.push_back(vec);
|
||||
}
|
||||
|
||||
size_t query_cnt = 200U;
|
||||
auto knnCtx = streamer->create_context();
|
||||
|
||||
float epison = 1e-6;
|
||||
for (size_t i = 0; i < query_cnt; i++) {
|
||||
auto &vec = vecs[i];
|
||||
std::string new_query;
|
||||
IndexQueryMeta new_meta;
|
||||
ASSERT_EQ(0, reformer->transform(vec.data(), qmeta, &new_query, &new_meta));
|
||||
|
||||
size_t topk = 50;
|
||||
knnCtx->set_topk(topk);
|
||||
ASSERT_EQ(0, streamer->search_impl(new_query.data(), new_meta, knnCtx));
|
||||
auto &results = knnCtx->result();
|
||||
ASSERT_EQ(topk, results.size());
|
||||
ASSERT_EQ(i, results[0].key());
|
||||
ASSERT_NEAR(0, results[0].score(), epison);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST_F(HnswStreamerTest, TestAddAndSearchWithID) {
|
||||
IndexStreamer::Pointer streamer =
|
||||
IndexFactory::CreateStreamer("HnswStreamer");
|
||||
@@ -3647,131 +3560,6 @@ TEST_F(HnswStreamerTest, TestAddAndSearchWithID) {
|
||||
// EXPECT_GT(cost, 2.0f);
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST_F(HnswStreamerTest, TestBasicRefiner) {
|
||||
uint32_t dimension = 1120;
|
||||
|
||||
IndexStreamer::Pointer base_streamer =
|
||||
IndexFactory::CreateStreamer("HnswStreamer");
|
||||
ASSERT_TRUE(base_streamer != nullptr);
|
||||
|
||||
IndexStreamer::Pointer refine_streamer =
|
||||
IndexFactory::CreateStreamer("FlatStreamer");
|
||||
ASSERT_TRUE(refine_streamer != nullptr);
|
||||
|
||||
IndexRefiner::Pointer refiner = IndexFactory::CreateRefiner("BasicRefiner");
|
||||
ASSERT_TRUE(refiner != nullptr);
|
||||
|
||||
ailego::Params params;
|
||||
IndexMeta index_meta(IndexMeta::DataType::DT_FP32, dimension);
|
||||
index_meta.set_metric("InnerProduct", 0, ailego::Params());
|
||||
|
||||
ailego::Params converter_params;
|
||||
auto converter = IndexFactory::CreateConverter("BinaryConverter");
|
||||
ASSERT_TRUE(converter != nullptr);
|
||||
|
||||
converter->init(index_meta, converter_params);
|
||||
|
||||
IndexMeta index_meta_binary = converter->meta();
|
||||
|
||||
auto reformer =
|
||||
IndexFactory::CreateReformer(index_meta_binary.reformer_name());
|
||||
ASSERT_TRUE(reformer != nullptr);
|
||||
|
||||
ASSERT_EQ(0, reformer->init(index_meta_binary.reformer_params()));
|
||||
|
||||
// base streamer
|
||||
ailego::Params base_stg_params;
|
||||
auto base_storage = IndexFactory::CreateStorage("MMapFileStorage");
|
||||
ASSERT_EQ(0, base_storage->init(base_stg_params));
|
||||
ASSERT_EQ(0, base_storage->open(dir_ + "TestBasicRefinerBase.index", true));
|
||||
ASSERT_EQ(0, base_streamer->init(index_meta_binary, params));
|
||||
ASSERT_EQ(0, base_streamer->open(base_storage));
|
||||
|
||||
auto base_ctx = base_streamer->create_context();
|
||||
ASSERT_TRUE(!!base_ctx);
|
||||
|
||||
// refine streamer
|
||||
ailego::Params refine_stg_params;
|
||||
auto refine_storage = IndexFactory::CreateStorage("MMapFileStorage");
|
||||
ASSERT_EQ(0, refine_storage->init(refine_stg_params));
|
||||
ASSERT_EQ(0,
|
||||
refine_storage->open(dir_ + "TestBasicRefinerRefine.index", true));
|
||||
ASSERT_EQ(0, refine_streamer->init(index_meta, params));
|
||||
ASSERT_EQ(0, refine_streamer->open(refine_storage));
|
||||
auto refine_ctx = refine_streamer->create_context();
|
||||
ASSERT_TRUE(!!refine_ctx);
|
||||
|
||||
ailego::Params refiner_params;
|
||||
ASSERT_EQ(0, refiner->init(base_streamer, refine_streamer, refiner_params));
|
||||
|
||||
auto ctx = refiner->create_context();
|
||||
ASSERT_TRUE(!!ctx);
|
||||
|
||||
IndexQueryMeta qmeta(IndexMeta::DataType::DT_FP32, dimension);
|
||||
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
std::uniform_real_distribution<float> dist(-2.0, 2.0);
|
||||
std::vector<NumericalVector<float>> vecs;
|
||||
|
||||
size_t cnt = 5000U;
|
||||
for (size_t i = 0; i < cnt; i++) {
|
||||
NumericalVector<float> vec(dimension);
|
||||
for (size_t j = 0; j < dimension; ++j) {
|
||||
vec[j] = dist(gen);
|
||||
}
|
||||
|
||||
std::string binary_vec;
|
||||
IndexQueryMeta binary_qmeta;
|
||||
|
||||
ASSERT_EQ(0,
|
||||
reformer->convert(vec.data(), qmeta, &binary_vec, &binary_qmeta));
|
||||
ASSERT_EQ(0, refiner->add_impl(i, binary_vec.data(), binary_qmeta,
|
||||
vec.data(), qmeta, ctx));
|
||||
|
||||
vecs.push_back(vec);
|
||||
}
|
||||
|
||||
size_t query_cnt = 200U;
|
||||
// size_t query_cnt = 1U;
|
||||
|
||||
auto searcherCtx = refiner->create_context();
|
||||
|
||||
for (size_t i = 0; i < query_cnt; i++) {
|
||||
auto &vec = vecs[i];
|
||||
|
||||
// float abs_value{0};
|
||||
// for (size_t j = 0; j < dimension; ++j) {
|
||||
// std::cout << "dim: " << j << ", value: " << vec[j] << std::endl;
|
||||
|
||||
// abs_value += std::abs(vec[j]);
|
||||
// }
|
||||
// std::cout << "abs value: " << abs_value << std::endl;
|
||||
|
||||
std::string new_query;
|
||||
IndexQueryMeta binary_qmeta;
|
||||
ASSERT_EQ(
|
||||
0, reformer->transform(vec.data(), qmeta, &new_query, &binary_qmeta));
|
||||
|
||||
size_t topk = 50;
|
||||
searcherCtx->set_topk(topk);
|
||||
ASSERT_EQ(0, refiner->search_impl(new_query.data(), binary_qmeta,
|
||||
vec.data(), qmeta, searcherCtx));
|
||||
auto &results = searcherCtx->result();
|
||||
ASSERT_EQ(topk, results.size());
|
||||
ASSERT_EQ(i, results[0].key());
|
||||
|
||||
// for (size_t i = 0; i < results.size(); ++i) {
|
||||
// std::cout << i << ", id: " << results[i].index()
|
||||
// << ", score: " << results[i].score() << std::endl;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
TEST_F(HnswStreamerTest, TestContiguousMemorySearch) {
|
||||
// Build index with mmap mode
|
||||
auto storage = IndexFactory::CreateStorage("MMapFileStorage");
|
||||
|
||||
@@ -1405,258 +1405,6 @@ TEST_F(IVFSearcherTest, TestRowMajorInt8WithBuildMemory) {
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(IVFSearcherTest, TestColumnMajorBinaryWithBuildMemory) {
|
||||
IVFBuilder builder;
|
||||
dimension_ = 256;
|
||||
index_meta_.set_meta(IndexMeta::DataType::DT_BINARY32, dimension_);
|
||||
index_meta_.set_metric("Hamming", 0, Params());
|
||||
index_meta_.set_major_order(IndexMeta::MO_COLUMN);
|
||||
|
||||
int ret = builder.init(index_meta_, params_);
|
||||
EXPECT_EQ(0, ret);
|
||||
size_t fnum = 257;
|
||||
prepare_binary_index_holder(0, fnum);
|
||||
ret = builder.train(threads_, holder_);
|
||||
EXPECT_EQ(0, ret);
|
||||
ret = builder.build(threads_, holder_);
|
||||
EXPECT_EQ(0, ret);
|
||||
IndexDumper::Pointer dumper = IndexFactory::CreateDumper("FileDumper");
|
||||
ret = dumper->create(index_path_);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = builder.dump(dumper);
|
||||
EXPECT_EQ(0, dumper->close());
|
||||
EXPECT_EQ((size_t)fnum, builder.stats().built_count());
|
||||
EXPECT_EQ((size_t)fnum, builder.stats().dumped_count());
|
||||
EXPECT_EQ((size_t)0, builder.stats().discarded_count());
|
||||
|
||||
IVFSearcher searcher;
|
||||
Params params;
|
||||
params.set(PARAM_IVF_SEARCHER_SCAN_RATIO, 1.0);
|
||||
params.set(PARAM_IVF_SEARCHER_BRUTE_FORCE_THRESHOLD, 1);
|
||||
|
||||
ret = searcher.init(params);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
IndexStorage::Pointer container =
|
||||
IndexFactory::CreateStorage("MMapFileReadStorage");
|
||||
EXPECT_TRUE(!!container);
|
||||
|
||||
Params container_params;
|
||||
container_params.set("proxima.mmap_file.container.memory_warmup", true);
|
||||
container->init(container_params);
|
||||
ret = container->open(index_path_, false);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = searcher.load(container, IndexMetric::Pointer());
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
BinaryVector<uint32_t> query(dimension_);
|
||||
for (size_t i = 0; i < dimension_; ++i) {
|
||||
query.set(i);
|
||||
}
|
||||
|
||||
size_t qnum = 63;
|
||||
BinaryVector<uint32_t> query1(dimension_ * qnum);
|
||||
for (size_t i = 0; i < qnum; ++i) {
|
||||
for (size_t j = 0; j < dimension_ && j < i; ++j) {
|
||||
query1.set(i * dimension_ + j);
|
||||
}
|
||||
}
|
||||
|
||||
auto context = searcher.create_context();
|
||||
IndexQueryMeta qmeta(IndexMeta::DataType::DT_BINARY32, dimension_);
|
||||
|
||||
// single bf serch
|
||||
{
|
||||
size_t topk = 128;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_bf_impl(query.data(), qmeta, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
const IndexDocumentList &result = context->result(0);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
for (size_t i = 0; i < topk; ++i) {
|
||||
EXPECT_EQ((uint64_t)256 - i, result[i].key());
|
||||
EXPECT_FLOAT_EQ((float)i, result[i].score());
|
||||
}
|
||||
}
|
||||
|
||||
// batch bf serch
|
||||
{
|
||||
size_t topk = 1;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_bf_impl(query1.data(), qmeta, qnum, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
for (size_t q = 0; q < qnum; ++q) {
|
||||
const IndexDocumentList &result = context->result(q);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
EXPECT_EQ((uint64_t)q, result[0].key());
|
||||
EXPECT_FLOAT_EQ((float)0, result[0].score());
|
||||
}
|
||||
}
|
||||
|
||||
// single knn search
|
||||
{
|
||||
size_t topk = 100;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_impl(query.data(), qmeta, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
const IndexDocumentList &result = context->result(0);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
for (size_t i = 0; i < topk; ++i) {
|
||||
EXPECT_EQ((uint64_t)256 - i, result[i].key());
|
||||
EXPECT_FLOAT_EQ((float)i, result[i].score());
|
||||
}
|
||||
}
|
||||
|
||||
// batch knn serch
|
||||
{
|
||||
size_t topk = 1;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_impl(query1.data(), qmeta, qnum, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
for (size_t q = 0; q < qnum; ++q) {
|
||||
const IndexDocumentList &result = context->result(q);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
EXPECT_EQ((uint64_t)q, result[0].key());
|
||||
EXPECT_FLOAT_EQ((float)0, result[0].score());
|
||||
}
|
||||
}
|
||||
|
||||
ret = searcher.unload();
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(IVFSearcherTest, TestRowMajorBinaryWithBuildMemory) {
|
||||
IVFBuilder builder;
|
||||
dimension_ = 256;
|
||||
index_meta_.set_meta(IndexMeta::DataType::DT_BINARY32, dimension_);
|
||||
index_meta_.set_metric("Hamming", 0, Params());
|
||||
index_meta_.set_major_order(IndexMeta::MO_ROW);
|
||||
|
||||
int ret = builder.init(index_meta_, params_);
|
||||
EXPECT_EQ(0, ret);
|
||||
size_t fnum = 257;
|
||||
prepare_binary_index_holder(0, fnum);
|
||||
ret = builder.train(threads_, holder_);
|
||||
EXPECT_EQ(0, ret);
|
||||
ret = builder.build(threads_, holder_);
|
||||
EXPECT_EQ(0, ret);
|
||||
IndexDumper::Pointer dumper = IndexFactory::CreateDumper("FileDumper");
|
||||
ret = dumper->create(index_path_);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = builder.dump(dumper);
|
||||
EXPECT_EQ(0, dumper->close());
|
||||
EXPECT_EQ((size_t)fnum, builder.stats().built_count());
|
||||
EXPECT_EQ((size_t)fnum, builder.stats().dumped_count());
|
||||
EXPECT_EQ((size_t)0, builder.stats().discarded_count());
|
||||
|
||||
IVFSearcher searcher;
|
||||
Params params;
|
||||
params.set(PARAM_IVF_SEARCHER_SCAN_RATIO, 1.0);
|
||||
params.set(PARAM_IVF_SEARCHER_BRUTE_FORCE_THRESHOLD, 1);
|
||||
|
||||
ret = searcher.init(params);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
IndexStorage::Pointer container =
|
||||
IndexFactory::CreateStorage("MMapFileReadStorage");
|
||||
EXPECT_TRUE(!!container);
|
||||
|
||||
Params container_params;
|
||||
container_params.set("proxima.mmap_file.container.memory_warmup", true);
|
||||
container->init(container_params);
|
||||
ret = container->open(index_path_, false);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
ret = searcher.load(container, IndexMetric::Pointer());
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
BinaryVector<uint32_t> query(dimension_);
|
||||
for (size_t i = 0; i < dimension_; ++i) {
|
||||
query.set(i);
|
||||
}
|
||||
|
||||
size_t qnum = 63;
|
||||
BinaryVector<uint32_t> query1(dimension_ * qnum);
|
||||
for (size_t i = 0; i < qnum; ++i) {
|
||||
for (size_t j = 0; j < dimension_ && j < i; ++j) {
|
||||
query1.set(i * dimension_ + j);
|
||||
}
|
||||
}
|
||||
|
||||
auto context = searcher.create_context();
|
||||
IndexQueryMeta qmeta(IndexMeta::DataType::DT_BINARY32, dimension_);
|
||||
|
||||
// single bf serch
|
||||
{
|
||||
size_t topk = 128;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_bf_impl(query.data(), qmeta, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
const IndexDocumentList &result = context->result(0);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
for (size_t i = 0; i < topk; ++i) {
|
||||
EXPECT_EQ((uint64_t)256 - i, result[i].key());
|
||||
EXPECT_FLOAT_EQ((float)i, result[i].score());
|
||||
}
|
||||
}
|
||||
|
||||
// batch bf serch
|
||||
{
|
||||
size_t topk = 1;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_bf_impl(query1.data(), qmeta, qnum, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
for (size_t q = 0; q < qnum; ++q) {
|
||||
const IndexDocumentList &result = context->result(q);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
EXPECT_EQ((uint64_t)q, result[0].key());
|
||||
EXPECT_FLOAT_EQ((float)0, result[0].score());
|
||||
}
|
||||
}
|
||||
|
||||
// single knn search
|
||||
{
|
||||
size_t topk = 100;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_impl(query.data(), qmeta, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
const IndexDocumentList &result = context->result(0);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
for (size_t i = 0; i < topk; ++i) {
|
||||
EXPECT_EQ((uint64_t)256 - i, result[i].key());
|
||||
EXPECT_FLOAT_EQ((float)i, result[i].score());
|
||||
}
|
||||
}
|
||||
|
||||
// batch knn serch
|
||||
{
|
||||
size_t topk = 1;
|
||||
context->set_topk(topk);
|
||||
ret = searcher.search_impl(query1.data(), qmeta, qnum, context);
|
||||
EXPECT_EQ(0, ret);
|
||||
|
||||
for (size_t q = 0; q < qnum; ++q) {
|
||||
const IndexDocumentList &result = context->result(q);
|
||||
EXPECT_EQ((size_t)topk, result.size());
|
||||
EXPECT_EQ((uint64_t)q, result[0].key());
|
||||
EXPECT_FLOAT_EQ((float)0, result[0].score());
|
||||
}
|
||||
}
|
||||
|
||||
ret = searcher.unload();
|
||||
EXPECT_EQ(0, ret);
|
||||
}
|
||||
|
||||
TEST_F(IVFSearcherTest, TestSearchWithEmptyCentroid) {
|
||||
IVFBuilder builder;
|
||||
Params params;
|
||||
|
||||
@@ -25,10 +25,6 @@ TEST(SquaredEuclideanMetric, General) {
|
||||
IndexMeta meta;
|
||||
meta.set_meta(IndexMeta::DataType::DT_INT16, 64);
|
||||
ASSERT_NE(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY32, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY64, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP16, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP32, 64);
|
||||
@@ -88,10 +84,6 @@ TEST(EuclideanMetric, General) {
|
||||
IndexMeta meta;
|
||||
meta.set_meta(IndexMeta::DataType::DT_INT16, 64);
|
||||
ASSERT_NE(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY32, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY64, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP16, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP32, 64);
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
// Copyright 2025-present the zvec project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <iostream>
|
||||
#include <gtest/gtest.h>
|
||||
#include "zvec/core/framework/index_factory.h"
|
||||
|
||||
using namespace zvec;
|
||||
using namespace zvec::core;
|
||||
|
||||
TEST(HammingMetric, General) {
|
||||
auto metric = IndexFactory::CreateMetric("Hamming");
|
||||
ASSERT_TRUE(metric);
|
||||
|
||||
IndexMeta meta;
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY32, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY64, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP16, 64);
|
||||
ASSERT_NE(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_FP32, 64);
|
||||
ASSERT_NE(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_INT8, 64);
|
||||
ASSERT_NE(0, metric->init(meta, ailego::Params()));
|
||||
meta.set_meta(IndexMeta::DataType::DT_BINARY32, 64);
|
||||
ASSERT_EQ(0, metric->init(meta, ailego::Params()));
|
||||
|
||||
IndexMeta meta2;
|
||||
meta2.set_meta(IndexMeta::DataType::DT_INT8, 64);
|
||||
EXPECT_TRUE(metric->is_matched(meta));
|
||||
EXPECT_FALSE(metric->is_matched(meta2));
|
||||
EXPECT_TRUE(metric->is_matched(
|
||||
meta, IndexQueryMeta(IndexMeta::DataType::DT_BINARY32, 64)));
|
||||
EXPECT_FALSE(metric->is_matched(
|
||||
meta, IndexQueryMeta(IndexMeta::DataType::DT_BINARY32, 63)));
|
||||
|
||||
EXPECT_FALSE(metric->distance_matrix(0, 0));
|
||||
EXPECT_FALSE(metric->distance_matrix(3, 5));
|
||||
EXPECT_FALSE(metric->distance_matrix(31, 65));
|
||||
EXPECT_TRUE(metric->distance_matrix(1, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(2, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(2, 2));
|
||||
EXPECT_TRUE(metric->distance_matrix(4, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(4, 2));
|
||||
EXPECT_TRUE(metric->distance_matrix(4, 4));
|
||||
EXPECT_TRUE(metric->distance_matrix(8, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(8, 2));
|
||||
EXPECT_TRUE(metric->distance_matrix(8, 4));
|
||||
EXPECT_TRUE(metric->distance_matrix(8, 8));
|
||||
EXPECT_TRUE(metric->distance_matrix(16, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(16, 2));
|
||||
EXPECT_TRUE(metric->distance_matrix(16, 4));
|
||||
EXPECT_TRUE(metric->distance_matrix(16, 8));
|
||||
EXPECT_TRUE(metric->distance_matrix(16, 16));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 1));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 2));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 4));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 8));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 16));
|
||||
EXPECT_TRUE(metric->distance_matrix(32, 32));
|
||||
|
||||
EXPECT_FALSE(metric->support_normalize());
|
||||
float result = 1.0f;
|
||||
metric->normalize(&result);
|
||||
EXPECT_FLOAT_EQ(1.0f, result);
|
||||
}
|
||||
@@ -108,28 +108,6 @@ class IndexMetaHelper {
|
||||
std::cerr << "Not supported type(" << type << ") for IP" << std::endl;
|
||||
return false;
|
||||
}
|
||||
} else if (method == std::string("Cosine")) {
|
||||
if (feature_type == IndexMeta::DataType::DT_FP32) {
|
||||
meta.set_metric("Cosine", 0, std::move(params));
|
||||
} else if (feature_type == IndexMeta::DataType::DT_INT8) {
|
||||
meta.set_metric("Cosine", 0, std::move(params));
|
||||
} else if (feature_type == IndexMeta::DataType::DT_FP16) {
|
||||
meta.set_metric("Cosine", 0, std::move(params));
|
||||
} else {
|
||||
std::cerr << "Not supported type(" << type << ") for Cosine"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
} else if (method == std::string("HAMMING")) {
|
||||
if (feature_type == IndexMeta::DataType::DT_BINARY32) {
|
||||
meta.set_metric("Hamming", 0, std::move(params));
|
||||
} else if (feature_type == IndexMeta::DataType::DT_BINARY64) {
|
||||
meta.set_metric("Hamming", 0, std::move(params));
|
||||
} else {
|
||||
std::cerr << "Not supported type(" << type << ") for hamming"
|
||||
<< std::endl;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
std::cerr << "Not supported method: " << method << std::endl;
|
||||
return false;
|
||||
|
||||
@@ -29,7 +29,7 @@ DEFINE_string(input_second_sep, " ", "input second sep");
|
||||
DEFINE_string(output, "output.vecs", "vecs output file");
|
||||
DEFINE_string(type, "float",
|
||||
"available type: float, double, int16, int8, binary");
|
||||
DEFINE_string(method, "L2", "available method: L2, IP, HAMMING");
|
||||
DEFINE_string(method, "L2", "available method: L2, IP");
|
||||
DEFINE_int32(dimension, 256, "data dimension");
|
||||
DEFINE_string(vector_type, "dense", "available type: dense, hybrid, sparse");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user