Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Support get vector data from index for HNSW (#200)
Browse files Browse the repository at this point in the history
Signed-off-by: yudong.cai <[email protected]>
  • Loading branch information
cydrain authored May 31, 2022
1 parent a993cf2 commit 7165744
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
23 changes: 15 additions & 8 deletions knowhere/index/vector_index/IndexHNSW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@

namespace knowhere {

// void
// normalize_vector(float* data, float* norm_array, size_t dim) {
// float norm = 0.0f;
// for (int i = 0; i < dim; i++) norm += data[i] * data[i];
// norm = 1.0f / (sqrtf(norm) + 1e-30f);
// for (int i = 0; i < dim; i++) norm_array[i] = data[i] * norm;
// }

BinarySet
IndexHNSW::Serialize(const Config& config) {
if (!index_) {
Expand Down Expand Up @@ -127,6 +119,21 @@ IndexHNSW::AddWithoutIds(const DatasetPtr& dataset_ptr, const Config& config) {
// LOG_KNOWHERE_DEBUG_ << GetStatistics()->ToString();
}

DatasetPtr
IndexHNSW::GetVectorById(const DatasetPtr& dataset_ptr, const Config& config) {
if (!index_) {
KNOWHERE_THROW_MSG("index not initialize");
}

GET_DATA_WITH_IDS(dataset_ptr)

float* p_x = (float*)malloc(sizeof(float) * dim * rows);
for (int64_t i = 0; i < rows; i++) {
memcpy(p_x + i * dim, index_->getDataByInternalId(p_ids[i]), dim * sizeof(float));
}
return GenResultDataset(p_x);
}

DatasetPtr
IndexHNSW::Query(const DatasetPtr& dataset_ptr, const Config& config, const faiss::BitsetView bitset) {
if (!index_) {
Expand Down
3 changes: 3 additions & 0 deletions knowhere/index/vector_index/IndexHNSW.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class IndexHNSW : public VecIndex {
void
AddWithoutIds(const DatasetPtr&, const Config&) override;

DatasetPtr
GetVectorById(const DatasetPtr&, const Config&) override;

DatasetPtr
Query(const DatasetPtr&, const Config&, const faiss::BitsetView) override;

Expand Down
7 changes: 5 additions & 2 deletions unittest/test_hnsw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ TEST_P(HNSWTest, HNSW_basic) {

index_->Load(bs);

auto result = index_->GetVectorById(id_dataset, conf_);
AssertVec(result, base_dataset, id_dataset, nq, dim);

auto adapter = knowhere::AdapterMgr::GetInstance().GetAdapter(index_type_);
ASSERT_TRUE(adapter->CheckSearch(conf_, index_type_, index_mode_));

auto result = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result, nq, k);
auto result1 = index_->Query(query_dataset, conf_, nullptr);
AssertAnns(result1, nq, k);

auto result2 = index_->Query(query_dataset, conf_, *bitset);
AssertAnns(result2, nq, k, CheckMode::CHECK_NOT_EQUAL);
Expand Down

0 comments on commit 7165744

Please sign in to comment.