|
| 1 | +// Copyright 2025-present the zvec project |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include <cstdint> |
| 16 | +#include <zvec/core/framework/index_error.h> |
| 17 | +#include <zvec/core/framework/index_factory.h> |
| 18 | +#include "metric_params.h" |
| 19 | + |
| 20 | +namespace zvec { |
| 21 | +namespace core { |
| 22 | + |
| 23 | +namespace { |
| 24 | + |
| 25 | +void UniformUint8SquaredEuclidean(const void *lhs_data, const void *rhs_data, |
| 26 | + size_t dimension, float *distance) { |
| 27 | + const auto *lhs = static_cast<const int8_t *>(lhs_data); |
| 28 | + const auto *rhs = static_cast<const int8_t *>(rhs_data); |
| 29 | + int64_t sum = 0; |
| 30 | + for (size_t i = 0; i < dimension; ++i) { |
| 31 | + // Both operands store uint8 codes shifted by -128. Widen before |
| 32 | + // subtraction so the full [-255, 255] difference remains exact. |
| 33 | + const int difference = static_cast<int>(lhs[i]) - static_cast<int>(rhs[i]); |
| 34 | + sum += static_cast<int64_t>(difference) * difference; |
| 35 | + } |
| 36 | + *distance = static_cast<float>(sum); |
| 37 | +} |
| 38 | + |
| 39 | +void UniformUint8SquaredEuclideanBatch(const void *const *vectors, |
| 40 | + const void *query, size_t count, |
| 41 | + size_t dimension, float *distances) { |
| 42 | + for (size_t i = 0; i < count; ++i) { |
| 43 | + UniformUint8SquaredEuclidean(vectors[i], query, dimension, distances + i); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +} // namespace |
| 48 | + |
| 49 | +class UniformUint8Metric : public IndexMetric { |
| 50 | + public: |
| 51 | + int init(const IndexMeta &meta, const ailego::Params ¶ms) override { |
| 52 | + if (meta.data_type() != IndexMeta::DataType::DT_INT8) { |
| 53 | + LOG_ERROR("UniformUint8Metric: unsupported type %d", meta.data_type()); |
| 54 | + return IndexError_Unsupported; |
| 55 | + } |
| 56 | + |
| 57 | + std::string metric_name; |
| 58 | + params.get(UNIFORM_UINT8_METRIC_ORIGIN_METRIC_NAME, &metric_name); |
| 59 | + if (metric_name.empty()) { |
| 60 | + LOG_ERROR("UniformUint8Metric: param %s is required", |
| 61 | + UNIFORM_UINT8_METRIC_ORIGIN_METRIC_NAME.c_str()); |
| 62 | + return IndexError_InvalidArgument; |
| 63 | + } |
| 64 | + if (metric_name != "SquaredEuclidean") { |
| 65 | + LOG_ERROR("UniformUint8Metric: only SquaredEuclidean supported, got %s", |
| 66 | + metric_name.c_str()); |
| 67 | + return IndexError_Unsupported; |
| 68 | + } |
| 69 | + |
| 70 | + meta_ = meta; |
| 71 | + params_ = params; |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + int cleanup(void) override { |
| 76 | + return 0; |
| 77 | + } |
| 78 | + |
| 79 | + bool is_matched(const IndexMeta &meta) const override { |
| 80 | + return meta.data_type() == meta_.data_type() && |
| 81 | + meta.unit_size() == meta_.unit_size() && |
| 82 | + meta.dimension() == meta_.dimension(); |
| 83 | + } |
| 84 | + |
| 85 | + bool is_matched(const IndexMeta &meta, |
| 86 | + const IndexQueryMeta &query_meta) const override { |
| 87 | + return is_matched(meta) && query_meta.data_type() == meta_.data_type() && |
| 88 | + query_meta.unit_size() == meta_.unit_size() && |
| 89 | + query_meta.dimension() == meta_.dimension(); |
| 90 | + } |
| 91 | + |
| 92 | + MatrixDistance distance(void) const override { |
| 93 | + return UniformUint8SquaredEuclidean; |
| 94 | + } |
| 95 | + |
| 96 | + MatrixDistance distance_matrix(size_t rows, size_t columns) const override { |
| 97 | + return rows == 1 && columns == 1 ? UniformUint8SquaredEuclidean : nullptr; |
| 98 | + } |
| 99 | + |
| 100 | + MatrixBatchDistance batch_distance(void) const override { |
| 101 | + return UniformUint8SquaredEuclideanBatch; |
| 102 | + } |
| 103 | + |
| 104 | + const ailego::Params ¶ms(void) const override { |
| 105 | + return params_; |
| 106 | + } |
| 107 | + |
| 108 | + int train(const void * /*vector*/, size_t /*dimension*/) override { |
| 109 | + return 0; |
| 110 | + } |
| 111 | + |
| 112 | + bool support_train(void) const override { |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + void normalize(float * /*score*/) const override {} |
| 117 | + |
| 118 | + bool support_normalize(void) const override { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + Pointer query_metric(void) const override { |
| 123 | + return nullptr; |
| 124 | + } |
| 125 | + |
| 126 | + private: |
| 127 | + IndexMeta meta_{}; |
| 128 | + ailego::Params params_{}; |
| 129 | +}; |
| 130 | + |
| 131 | +INDEX_FACTORY_REGISTER_METRIC_ALIAS(UniformUint8, UniformUint8Metric); |
| 132 | + |
| 133 | +} // namespace core |
| 134 | +} // namespace zvec |
0 commit comments