-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zhongxiaoyao.zxy <[email protected]>
- Loading branch information
1 parent
e87defc
commit 0d51c38
Showing
11 changed files
with
675 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
|
||
// Copyright 2024-present the vsag 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 "algorithm/hnswlib/hnswalg.h" | ||
#include "algorithm/hnswlib/space_l2.h" | ||
|
||
namespace vsag { | ||
class AdaptGraphDataCell { | ||
public: | ||
AdaptGraphDataCell(std::shared_ptr<hnswlib::HierarchicalNSW> alg_hnsw) : alg_hnsw_(alg_hnsw){}; | ||
|
||
void | ||
GetNeighbors(InnerIdType id, Vector<InnerIdType>& neighbor_ids) { | ||
int* data = (int*)alg_hnsw_->get_linklist0(id); | ||
uint32_t size = alg_hnsw_->getListCount((hnswlib::linklistsizeint*)data); | ||
neighbor_ids.resize(size); | ||
for (uint32_t i = 0; i < size; i++) { | ||
neighbor_ids[i] = *(data + i + 1); | ||
} | ||
} | ||
|
||
uint32_t | ||
GetNeighborSize(InnerIdType id) { | ||
int* data = (int*)alg_hnsw_->get_linklist0(id); | ||
return alg_hnsw_->getListCount((hnswlib::linklistsizeint*)data); | ||
} | ||
|
||
void | ||
Prefetch(InnerIdType id, InnerIdType neighbor_i) { | ||
int* data = (int*)alg_hnsw_->get_linklist0(id); | ||
_mm_prefetch(data + neighbor_i + 1, _MM_HINT_T0); | ||
} | ||
|
||
uint32_t | ||
MaximumDegree() { | ||
return alg_hnsw_->getMaxDegree(); | ||
} | ||
|
||
private: | ||
std::shared_ptr<hnswlib::HierarchicalNSW> alg_hnsw_; | ||
}; | ||
} // namespace vsag |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
// Copyright 2024-present the vsag 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 "adapter_graph_datacell.h" | ||
|
||
#include "catch2/catch_template_test_macros.hpp" | ||
#include "fixtures.h" | ||
#include "fmt/format-inl.h" | ||
#include "graph_interface_test.h" | ||
#include "io/io_headers.h" | ||
#include "safe_allocator.h" | ||
|
||
using namespace vsag; | ||
|
||
TEST_CASE("basic usage for graph data cell (adapter of hnsw)", "[ut][GraphDataCell]") { | ||
uint32_t M = 32; | ||
uint32_t data_size = 1000; | ||
uint32_t ef_construction = 100; | ||
uint64_t DEFAULT_MAX_ELEMENT = 1; | ||
uint64_t dim = 960; | ||
auto vectors = fixtures::generate_vectors(data_size, dim); | ||
std::vector<int64_t> ids(data_size); | ||
std::iota(ids.begin(), ids.end(), 0); | ||
|
||
auto allocator = SafeAllocator::FactoryDefaultAllocator(); | ||
auto space = std::make_shared<hnswlib::L2Space>(dim); | ||
auto io = std::make_shared<MemoryIO>(allocator.get()); | ||
auto alg_hnsw = | ||
std::make_shared<hnswlib::HierarchicalNSW>(space.get(), | ||
DEFAULT_MAX_ELEMENT, | ||
allocator.get(), | ||
M / 2, | ||
ef_construction, | ||
Options::Instance().block_size_limit()); | ||
alg_hnsw->init_memory_space(); | ||
for (int64_t i = 0; i < data_size; ++i) { | ||
auto successful_insert = | ||
alg_hnsw->addPoint((const void*)(vectors.data() + i * dim), ids[i]); | ||
REQUIRE(successful_insert == true); | ||
} | ||
|
||
auto graph_data_cell = std::make_shared<AdaptGraphDataCell>(alg_hnsw); | ||
|
||
for (uint32_t i = 0; i < data_size; i++) { | ||
auto neighbor_size = graph_data_cell->GetNeighborSize(i); | ||
Vector<InnerIdType> neighbor_ids(neighbor_size, allocator.get()); | ||
graph_data_cell->GetNeighbors(i, neighbor_ids); | ||
|
||
int* data = (int*)alg_hnsw->get_linklist0(i); | ||
REQUIRE(neighbor_size == alg_hnsw->getListCount((hnswlib::linklistsizeint*)data)); | ||
|
||
for (uint32_t j = 0; j < neighbor_size; j++) { | ||
REQUIRE(neighbor_ids[j] == *(data + j + 1)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.