Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/core/metric/quantized_integer_metric.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class QuantizedIntegerMetric : public IndexMetric {
if (meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_distance_func(
turbo::MetricType::kSquaredEuclidean, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret && m == 1 && n == 1) {
return turbo_ret;
}
Expand Down Expand Up @@ -139,7 +139,7 @@ class QuantizedIntegerMetric : public IndexMetric {
if (meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_distance_func(
turbo::MetricType::kCosine, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret) {
return turbo_ret;
}
Expand All @@ -160,7 +160,7 @@ class QuantizedIntegerMetric : public IndexMetric {
if (meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_batch_distance_func(
turbo::MetricType::kSquaredEuclidean, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret) {
return turbo_ret;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ class QuantizedIntegerMetric : public IndexMetric {
if (meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_batch_distance_func(
turbo::MetricType::kCosine, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret) {
return turbo_ret;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ class QuantizedIntegerMetric : public IndexMetric {
meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_query_preprocess_func(
turbo::MetricType::kCosine, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret) {
return turbo_ret;
}
Expand All @@ -310,7 +310,7 @@ class QuantizedIntegerMetric : public IndexMetric {
meta_.data_type() == IndexMeta::DataType::DT_INT8) {
auto turbo_ret = turbo::get_query_preprocess_func(
turbo::MetricType::kSquaredEuclidean, turbo::DataType::kInt8,
turbo::QuantizeType::kDefault);
turbo::QuantizeType::kRecord, turbo::CpuArchType::kAVX512VNNI);
if (turbo_ret) {
return turbo_ret;
}
Expand Down
20 changes: 19 additions & 1 deletion src/include/zvec/turbo/turbo.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ enum class DataType {
};

enum class QuantizeType {
kDefault,
//! Deprecated: no dispatch row serves kDefault anymore; request the
//! explicit quantize type (kFp32, kFp16, kRecord, ...) instead. The
//! enumerator is kept (value 0) for serialized-header compatibility.
kDefault [[deprecated("request an explicit QuantizeType instead")]],

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个枚举值删了吧?

kUniform,
kRecord,
kFp16,
Expand Down Expand Up @@ -129,6 +132,21 @@ ZVEC_TURBO_API QueryPreprocessFunc get_query_preprocess_func(
MetricType metric_type, DataType data_type, QuantizeType quantize_type,
CpuArchType cpu_arch_type = CpuArchType::kAuto);

// All kernels of a single dispatched kernel family. `preprocess` is non-null
// when the batch kernel requires the query to be preprocessed first (e.g.
// the AVX512-VNNI int8 kernels expect a +128 uint8-shifted query).
struct DistanceKernels {
DistanceFunc dist{};
BatchDistanceFunc batch{};
QueryPreprocessFunc preprocess = nullptr;
};

// Aggregate lookup: resolves dist/batch/preprocess in one pass so callers
// cannot pair functions from different kernel families.
DistanceKernels get_distance_kernels(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里为何不定义ZVEC_TURBO_API?

MetricType metric_type, DataType data_type, QuantizeType quantize_type,
CpuArchType cpu_arch_type = CpuArchType::kAuto);

// Returns the SIMD kernel for the uniform quantizer on the current CPU for
// the given output data_type, or nullptr if no SIMD implementation is
// available (callers must keep a scalar fallback). This is a
Expand Down
40 changes: 40 additions & 0 deletions src/turbo/distance/scalar/fp16/cosine.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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 "scalar/fp16/cosine.h"
#include "scalar/fp16/inner_product.h"

namespace zvec::turbo::scalar {

void cosine_fp16_distance(const void *a, const void *b, size_t dim,
float *distance) {
// inner_product_fp16_distance returns -real_IP; cosine = 1 - real_IP = 1 +
// ip.
float ip;
inner_product_fp16_distance(a, b, dim, &ip);

*distance = 1 + ip;
}

void cosine_fp16_batch_distance(const void *const *vectors, const void *query,
size_t n, size_t dim, float *distances) {
inner_product_fp16_batch_distance(vectors, query, n, dim, distances);
// inner_product batch returns -real_IP per element; cosine = 1 - real_IP = 1
// + d.
for (size_t i = 0; i < n; i++) {
distances[i] = 1 + distances[i];
}
}

} // namespace zvec::turbo::scalar
30 changes: 30 additions & 0 deletions src/turbo/distance/scalar/fp16/cosine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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 <cstddef>

namespace zvec::turbo::scalar {

// Compute cosine distance (negative inner product after normalization)
// between a single quantized FP16 vector pair.
void cosine_fp16_distance(const void *a, const void *b, size_t dim,
float *distance);

// Batch version of cosine_fp16_distance.
void cosine_fp16_batch_distance(const void *const *vectors, const void *query,
size_t n, size_t dim, float *distances);

} // namespace zvec::turbo::scalar
46 changes: 46 additions & 0 deletions src/turbo/distance/scalar/fp16/inner_product.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 "scalar/fp16/inner_product.h"
#include <cstdint>
#include <zvec/ailego/utility/float_helper.h>

namespace zvec::turbo::scalar {

// Compute negated inner product between a single FP16 vector pair.
// Returns -dot(a, b) so that callers can derive cosine distance as 1 + ip.
void inner_product_fp16_distance(const void *a, const void *b, size_t dim,
float *distance) {
const uint16_t *m = reinterpret_cast<const uint16_t *>(a);
const uint16_t *q = reinterpret_cast<const uint16_t *>(b);

float sum = 0.0f;
for (size_t i = 0; i < dim; ++i) {
sum += zvec::ailego::FloatHelper::ToFP32(m[i]) *
zvec::ailego::FloatHelper::ToFP32(q[i]);
}

*distance = -sum;
}

// Batch version of inner_product_fp16_distance.
void inner_product_fp16_batch_distance(const void *const *vectors,
const void *query, size_t n, size_t dim,
float *distances) {
for (size_t i = 0; i < n; ++i) {
inner_product_fp16_distance(vectors[i], query, dim, &distances[i]);
}
}

} // namespace zvec::turbo::scalar
32 changes: 32 additions & 0 deletions src/turbo/distance/scalar/fp16/inner_product.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 <cstddef>

namespace zvec::turbo::scalar {

// Compute inner product distance between a single quantized FP16
// vector pair. Returns -dot(a, b) so that callers can derive cosine
// distance as 1 + ip.
void inner_product_fp16_distance(const void *a, const void *b, size_t dim,
float *distance);

// Batch version of inner_product_fp16_distance.
void inner_product_fp16_batch_distance(const void *const *vectors,
const void *query, size_t n, size_t dim,
float *distances);

} // namespace zvec::turbo::scalar
45 changes: 45 additions & 0 deletions src/turbo/distance/scalar/fp16/squared_euclidean.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 "scalar/fp16/squared_euclidean.h"
#include <cstdint>
#include <ailego/utility/math_helper.h>
#include <zvec/ailego/utility/float_helper.h>

namespace zvec::turbo::scalar {

void squared_euclidean_fp16_distance(const void *a, const void *b, size_t dim,
float *distance) {
const uint16_t *m = reinterpret_cast<const uint16_t *>(a);
const uint16_t *q = reinterpret_cast<const uint16_t *>(b);

float sum = 0.0f;
for (size_t i = 0; i < dim; ++i) {
sum += zvec::ailego::MathHelper::SquaredDifference(
zvec::ailego::FloatHelper::ToFP32(m[i]),
zvec::ailego::FloatHelper::ToFP32(q[i]));
}

*distance = sum;
}

void squared_euclidean_fp16_batch_distance(const void *const *vectors,
const void *query, size_t n,
size_t dim, float *distances) {
for (size_t i = 0; i < n; ++i) {
squared_euclidean_fp16_distance(vectors[i], query, dim, &distances[i]);
}
}

} // namespace zvec::turbo::scalar
31 changes: 31 additions & 0 deletions src/turbo/distance/scalar/fp16/squared_euclidean.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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 <cstddef>

namespace zvec::turbo::scalar {

// Compute squared euclidean distance between a single quantized FP16
// vector pair.
void squared_euclidean_fp16_distance(const void *a, const void *b, size_t dim,
float *distance);

// Batch version of squared euclidean FP16.
void squared_euclidean_fp16_batch_distance(const void *const *vectors,
const void *query, size_t n,
size_t dim, float *distances);

} // namespace zvec::turbo::scalar
58 changes: 58 additions & 0 deletions src/turbo/distance/scalar/record_quantized_int4/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// 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.

// Shared scalar helpers for record_quantized_int4 distance implementations.
//
// Record-quantized INT4 layout (see core::RecordQuantizer::quantize_record):
//
// [ original_dim / 2 bytes: packed signed int4 elements (lo nibble first) ]
// [ float scale_a ] (a: 1 / scale)
// [ float bias_a ] (b: -bias / scale)
// [ float sum_a ] (s: sum of quantized codes)
// [ float square_sum_a ] (s2: sum of squared codes, euclidean metrics)
// or
// [ int int8_sum ] (sum of raw codes, non-euclidean metrics)
//
// Total tail size: 16 bytes = 32 int4 units. For the Cosine metric an
// additional fp32 norm is appended after the tail (20 bytes = 40 int4
// units); it is not used for distance computation.
//
// All `dim` arguments below are expressed in int4 units, following the core
// convention (element size in bytes is dim / 2).

#pragma once

#include <cstddef>
#include <cstdint>

namespace zvec::turbo::scalar::internal {

// Raw integer inner product of two packed signed int4 code arrays holding
// `size` int4 elements (`size` must be even).
inline float ip_int4_scalar(const void *a, const void *b, size_t size) {
const uint8_t *lhs = reinterpret_cast<const uint8_t *>(a);
const uint8_t *rhs = reinterpret_cast<const uint8_t *>(b);

float sum = 0.0f;
for (size_t i = 0; i < (size >> 1); ++i) {
int8_t m_lo = static_cast<int8_t>(lhs[i] << 4) >> 4;
int8_t m_hi = static_cast<int8_t>(lhs[i] & 0xf0) >> 4;
int8_t q_lo = static_cast<int8_t>(rhs[i] << 4) >> 4;
int8_t q_hi = static_cast<int8_t>(rhs[i] & 0xf0) >> 4;
sum += static_cast<float>(m_lo * q_lo + m_hi * q_hi);
}
return sum;
}

} // namespace zvec::turbo::scalar::internal
Loading
Loading