Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move read/writeBinaryPOD from hnswlib to knowhere utils. #194

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/knowhere/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,16 @@ ConvertIVFFlat(const BinarySet& binset, const MetricType metric_type, const uint
bool
UseDiskLoad(const std::string& index_type, const int32_t& /*version*/);

template <typename T, typename W>
static void
writeBinaryPOD(W& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T, typename R>
static void
readBinaryPOD(R& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

} // namespace knowhere
4 changes: 4 additions & 0 deletions thirdparty/hnswlib/hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "hnswlib.h"
#include "io/memory_io.h"
#include "knowhere/config.h"
#include "knowhere/utils.h"
#include "knowhere/heap.h"
#include "neighbor.h"
#include "visited_list_pool.h"
Expand Down Expand Up @@ -661,6 +662,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
loadIndex(const std::string& location, const knowhere::Config& config, size_t max_elements_i = 0) {
using knowhere::readBinaryPOD;
auto cfg = static_cast<const knowhere::BaseConfig&>(config);

auto input = knowhere::FileReader(location);
Expand Down Expand Up @@ -770,6 +772,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
saveIndex(knowhere::MemoryIOWriter& output) {
using knowhere::writeBinaryPOD;
// write l2/ip calculator
writeBinaryPOD(output, metric_type_);
writeBinaryPOD(output, data_size_);
Expand Down Expand Up @@ -807,6 +810,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t> {

void
loadIndex(knowhere::MemoryIOReader& input, size_t max_elements_i = 0) {
using knowhere::readBinaryPOD;
// linxj: init with metrictype
size_t dim;
readBinaryPOD(input, metric_type_);
Expand Down
24 changes: 0 additions & 24 deletions thirdparty/hnswlib/hnswlib/hnswlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,6 @@ class pairGreater {
}
};

template <typename T>
static void
writeBinaryPOD(std::ostream& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T>
static void
readBinaryPOD(std::istream& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

template <typename T, typename W>
static void
writeBinaryPOD(W& out, const T& podRef) {
out.write((char*)&podRef, sizeof(T));
}

template <typename T, typename R>
static void
readBinaryPOD(R& in, T& podRef) {
in.read((char*)&podRef, sizeof(T));
}

template <typename MTYPE>
using DISTFUNC = MTYPE (*)(const void*, const void*, const void*);

Expand Down
Loading