Skip to content

Commit d9c2862

Browse files
committed
modify
Signed-off-by: jinjiabao.jjb <jinjiabao.jjb@antgroup.com>
1 parent 81b2db3 commit d9c2862

File tree

11 files changed

+44
-247
lines changed

11 files changed

+44
-247
lines changed

src/algorithm/hnswlib/algorithm_interface.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class AlgorithmInterface {
5454
size_t ef,
5555
vsag::BaseFilterFunctor* isIdAllowed = nullptr) const;
5656

57-
virtual void
58-
saveIndex(const std::string& location) = 0;
59-
6057
virtual void
6158
saveIndex(void* d) = 0;
6259

src/algorithm/hnswlib/block_manager.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,6 @@ BlockManager::Serialize(std::ostream& ofs, size_t cur_element_count) {
9999
return this->SerializeImpl(writer, cur_element_count);
100100
}
101101

102-
bool
103-
BlockManager::Deserialize(std::function<void(uint64_t, uint64_t, void*)> read_func,
104-
uint64_t cursor,
105-
size_t cur_element_count) {
106-
ReadFuncStreamReader reader(read_func, cursor);
107-
return this->DeserializeImpl(reader, cur_element_count);
108-
}
109-
110102
bool
111103
BlockManager::Deserialize(std::istream& ifs, size_t cur_element_count) {
112104
IOStreamReader reader(ifs);

src/algorithm/hnswlib/block_manager.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,6 @@ class BlockManager {
4444
bool
4545
Serialize(std::ostream& ofs, size_t cur_element_count);
4646

47-
bool
48-
Deserialize(std::function<void(uint64_t, uint64_t, void*)> read_func,
49-
uint64_t cursor,
50-
size_t cur_element_count);
51-
5247
bool
5348
Deserialize(std::istream& ifs, size_t cur_element_count);
5449

src/algorithm/hnswlib/hnswalg.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -789,14 +789,6 @@ HierarchicalNSW::saveIndex(std::ostream& out_stream) {
789789
SerializeImpl(writer);
790790
}
791791

792-
void
793-
HierarchicalNSW::saveIndex(const std::string& location) {
794-
std::ofstream output(location, std::ios::binary);
795-
IOStreamWriter writer(output);
796-
SerializeImpl(writer);
797-
output.close();
798-
}
799-
800792
template <typename T>
801793
static void
802794
WriteOne(StreamWriter& writer, T& value) {
@@ -979,44 +971,6 @@ HierarchicalNSW::markDeletedInternal(InnerIdType internalId) {
979971
}
980972
}
981973

982-
/*
983-
* Removes the deleted mark of the node, does NOT really change the current graph.
984-
*
985-
* Note: the method is not safe to use when replacement of deleted elements is enabled,
986-
* because elements marked as deleted can be completely removed by addPoint
987-
*/
988-
void
989-
HierarchicalNSW::unmarkDelete(LabelType label) {
990-
// lock all operations with element by label
991-
std::unique_lock lock_table(label_lookup_lock_);
992-
auto search = label_lookup_.find(label);
993-
if (search == label_lookup_.end()) {
994-
throw std::runtime_error("Label not found");
995-
}
996-
InnerIdType internalId = search->second;
997-
unmarkDeletedInternal(internalId);
998-
}
999-
1000-
/*
1001-
* Remove the deleted mark of the node.
1002-
*/
1003-
void
1004-
HierarchicalNSW::unmarkDeletedInternal(InnerIdType internalId) {
1005-
assert(internalId < cur_element_count_);
1006-
if (isMarkedDeleted(internalId)) {
1007-
unsigned char* ll_cur =
1008-
(unsigned char*)data_level0_memory_->GetElementPtr(internalId, offsetLevel0_) + 2;
1009-
*ll_cur &= ~DELETE_MARK;
1010-
num_deleted_ -= 1;
1011-
if (allow_replace_deleted_) {
1012-
std::unique_lock<std::mutex> lock_deleted_elements(deleted_elements_lock_);
1013-
deleted_elements_.erase(internalId);
1014-
}
1015-
} else {
1016-
throw std::runtime_error("The requested to undelete element is not deleted");
1017-
}
1018-
}
1019-
1020974
/*
1021975
* Adds point.
1022976
*/
@@ -1513,39 +1467,4 @@ HierarchicalNSW::searchRange(const void* query_data,
15131467
// std::cout << "hnswalg::result.size(): " << result.size() << std::endl;
15141468
return result;
15151469
}
1516-
1517-
void
1518-
HierarchicalNSW::checkIntegrity() {
1519-
int connections_checked = 0;
1520-
vsag::Vector<int> inbound_connections_num(cur_element_count_, 0, allocator_);
1521-
for (int i = 0; i < cur_element_count_; i++) {
1522-
for (int l = 0; l <= element_levels_[i]; l++) {
1523-
auto data_ll_cur = getLinklistAtLevelWithLock(i, l);
1524-
linklistsizeint* ll_cur = (linklistsizeint*)data_ll_cur.get();
1525-
int size = getListCount(ll_cur);
1526-
auto* data = (InnerIdType*)(ll_cur + 1);
1527-
vsag::UnorderedSet<InnerIdType> s(allocator_);
1528-
for (int j = 0; j < size; j++) {
1529-
assert(data[j] > 0);
1530-
assert(data[j] < cur_element_count_);
1531-
assert(data[j] != i);
1532-
inbound_connections_num[data[j]]++;
1533-
s.insert(data[j]);
1534-
connections_checked++;
1535-
}
1536-
assert(s.size() == size);
1537-
}
1538-
}
1539-
if (cur_element_count_ > 1) {
1540-
int min1 = inbound_connections_num[0], max1 = inbound_connections_num[0];
1541-
for (int i = 0; i < cur_element_count_; i++) {
1542-
assert(inbound_connections_num[i] > 0);
1543-
min1 = std::min(inbound_connections_num[i], min1);
1544-
max1 = std::max(inbound_connections_num[i], max1);
1545-
}
1546-
std::cout << "Min inbound: " << min1 << ", Max inbound:" << max1 << "\n";
1547-
}
1548-
std::cout << "integrity ok, checked " << connections_checked << " connections\n";
1549-
}
1550-
15511470
} // namespace hnswlib

src/algorithm/hnswlib/hnswalg.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,6 @@ class HierarchicalNSW : public AlgorithmInterface<float> {
302302
void
303303
saveIndex(std::ostream& out_stream) override;
304304

305-
void
306-
saveIndex(const std::string& location) override;
307-
308305
void
309306
SerializeImpl(StreamWriter& writer);
310307

@@ -329,20 +326,6 @@ class HierarchicalNSW : public AlgorithmInterface<float> {
329326
void
330327
markDeletedInternal(InnerIdType internalId);
331328

332-
/*
333-
* Removes the deleted mark of the node, does NOT really change the current graph.
334-
*
335-
* Note: the method is not safe to use when replacement of deleted elements is enabled,
336-
* because elements marked as deleted can be completely removed by addPoint
337-
*/
338-
void
339-
unmarkDelete(LabelType label);
340-
/*
341-
* Remove the deleted mark of the node.
342-
*/
343-
void
344-
unmarkDeletedInternal(InnerIdType internalId);
345-
346329
/*
347330
* Checks the first 16 bits of the memory to see if the element is marked deleted.
348331
*/
@@ -405,9 +388,6 @@ class HierarchicalNSW : public AlgorithmInterface<float> {
405388
uint64_t ef,
406389
vsag::BaseFilterFunctor* isIdAllowed = nullptr) const override;
407390

408-
void
409-
checkIntegrity();
410-
411391
void
412392
reset();
413393

src/algorithm/hnswlib/hnswalg_static.h

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,39 +1144,6 @@ class StaticHierarchicalNSW : public AlgorithmInterface<float> {
11441144
out_stream.write((char*)node_cluster_dist_, max_elements_ * sizeof(float));
11451145
}
11461146

1147-
void
1148-
saveIndex(const std::string& location) override {
1149-
throw std::runtime_error("static hnsw does not support save index");
1150-
// std::ofstream output(location, std::ios::binary);
1151-
// std::streampos position;
1152-
//
1153-
// writeBinaryPOD(output, offsetLevel0_);
1154-
// writeBinaryPOD(output, max_elements_);
1155-
// writeBinaryPOD(output, cur_element_count_);
1156-
// writeBinaryPOD(output, size_data_per_element_);
1157-
// writeBinaryPOD(output, label_offset_);
1158-
// writeBinaryPOD(output, offsetData_);
1159-
// writeBinaryPOD(output, maxlevel_);
1160-
// writeBinaryPOD(output, enterpoint_node_);
1161-
// writeBinaryPOD(output, maxM_);
1162-
//
1163-
// writeBinaryPOD(output, maxM0_);
1164-
// writeBinaryPOD(output, M_);
1165-
// writeBinaryPOD(output, mult_);
1166-
// writeBinaryPOD(output, ef_construction_);
1167-
//
1168-
// output.write(data_level0_memory_, cur_element_count_ * size_data_per_element_);
1169-
//
1170-
// for (size_t i = 0; i < cur_element_count_; i++) {
1171-
// unsigned int linkListSize =
1172-
// element_levels_[i] > 0 ? size_links_per_element_ * element_levels_[i] : 0;
1173-
// writeBinaryPOD(output, linkListSize);
1174-
// if (linkListSize)
1175-
// output.write(linkLists_[i], linkListSize);
1176-
// }
1177-
// output.close();
1178-
}
1179-
11801147
// load index from a file stream
11811148
void
11821149
loadIndex(StreamReader& in_stream, SpaceInterface* s, size_t max_elements_i = 0) override {

src/algorithm/hnswlib/hnswlib.h

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -26,37 +26,6 @@
2626
#endif
2727
#endif
2828

29-
#if defined(USE_AVX) || defined(USE_SSE)
30-
#ifdef _MSC_VER
31-
#include <intrin.h>
32-
33-
#include <stdexcept>
34-
void
35-
cpuid(int32_t out[4], int32_t eax, int32_t ecx) {
36-
__cpuidex(out, eax, ecx);
37-
}
38-
static __int64
39-
xgetbv(unsigned int x) {
40-
return _xgetbv(x);
41-
}
42-
#else
43-
#include <cpuid.h>
44-
#include <x86intrin.h>
45-
46-
#include <cstdint>
47-
#include <future>
48-
static void
49-
cpuid(int32_t cpuInfo[4], int32_t eax, int32_t ecx) {
50-
__cpuid_count(eax, ecx, cpuInfo[0], cpuInfo[1], cpuInfo[2], cpuInfo[3]);
51-
}
52-
static uint64_t
53-
xgetbv(unsigned int index) {
54-
uint32_t eax, edx;
55-
__asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
56-
return ((uint64_t)edx << 32) | eax;
57-
}
58-
#endif
59-
6029
#if defined(USE_AVX512)
6130
#include <immintrin.h>
6231
#endif
@@ -69,69 +38,6 @@ xgetbv(unsigned int index) {
6938
#define PORTABLE_ALIGN64 __declspec(align(64))
7039
#endif
7140

72-
// Adapted from https://github.com/Mysticial/FeatureDetector
73-
#define _XCR_XFEATURE_ENABLED_MASK 0
74-
75-
static bool
76-
AVXCapable() {
77-
int cpuInfo[4];
78-
79-
// CPU support
80-
cpuid(cpuInfo, 0, 0);
81-
int nIds = cpuInfo[0];
82-
83-
bool HW_AVX = false;
84-
if (nIds >= 0x00000001) {
85-
cpuid(cpuInfo, 0x00000001, 0);
86-
HW_AVX = (cpuInfo[2] & ((int)1 << 28)) != 0;
87-
}
88-
89-
// OS support
90-
cpuid(cpuInfo, 1, 0);
91-
92-
bool osUsesXSAVE_XRSTORE = (cpuInfo[2] & (1 << 27)) != 0;
93-
bool cpuAVXSuport = (cpuInfo[2] & (1 << 28)) != 0;
94-
95-
bool avxSupported = false;
96-
if (osUsesXSAVE_XRSTORE && cpuAVXSuport) {
97-
uint64_t xcrFeatureMask = xgetbv(_XCR_XFEATURE_ENABLED_MASK);
98-
avxSupported = (xcrFeatureMask & 0x6) == 0x6;
99-
}
100-
return HW_AVX && avxSupported;
101-
}
102-
103-
static bool
104-
AVX512Capable() {
105-
if (!AVXCapable())
106-
return false;
107-
108-
int cpuInfo[4];
109-
110-
// CPU support
111-
cpuid(cpuInfo, 0, 0);
112-
int nIds = cpuInfo[0];
113-
114-
bool HW_AVX512F = false;
115-
if (nIds >= 0x00000007) { // AVX512 Foundation
116-
cpuid(cpuInfo, 0x00000007, 0);
117-
HW_AVX512F = (cpuInfo[1] & ((int)1 << 16)) != 0;
118-
}
119-
120-
// OS support
121-
cpuid(cpuInfo, 1, 0);
122-
123-
bool osUsesXSAVE_XRSTORE = (cpuInfo[2] & (1 << 27)) != 0;
124-
bool cpuAVXSuport = (cpuInfo[2] & (1 << 28)) != 0;
125-
126-
bool avx512Supported = false;
127-
if (osUsesXSAVE_XRSTORE && cpuAVXSuport) {
128-
uint64_t xcrFeatureMask = xgetbv(_XCR_XFEATURE_ENABLED_MASK);
129-
avx512Supported = (xcrFeatureMask & 0xe6) == 0xe6;
130-
}
131-
return HW_AVX512F && avx512Supported;
132-
}
133-
#endif
134-
13541
#include "hnswalg.h"
13642
#include "hnswalg_static.h"
13743
#include "space_ip.h"

src/default_thread_pool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ DefaultThreadPool::Enqueue(std::function<void(void)> func) {
2828

2929
void
3030
DefaultThreadPool::WaitUntilEmpty() {
31-
pool_->wait_until_empty();
31+
// In progschj::ThreadPool, wait_until_nothing_in_flight indicates that all tasks have been completed, while wait_until_empty means that there are no tasks waiting. Therefore, what we actually need here is the semantics of wait_until_nothing_in_flight.
32+
pool_->wait_until_nothing_in_flight();
3233
}
3334

3435
void

src/index/hnsw_test.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ TEST_CASE("static hnsw", "[ut][hnsw]") {
459459
REQUIRE_FALSE(remove_result.has_value());
460460
REQUIRE(remove_result.error().type == ErrorType::UNSUPPORTED_INDEX_OPERATION);
461461

462-
463462
SECTION("serialize to fstream") {
464463
fixtures::TempDir dir("hnsw_test_deserialize_on_not_empty_index");
465464
std::fstream out_stream(dir.path + "index.bin", std::ios::out | std::ios::binary);

src/safe_thread_pool_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
// Copyright 2024-present the vsag project
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include "safe_thread_pool.h"
17+
18+
#include <catch2/catch_test_macros.hpp>
19+
20+
TEST_CASE("test safe thread pool", "[ut][thread_pool]") {
21+
auto thread_pool = vsag::SafeThreadPool::FactoryDefaultThreadPool();
22+
int data = 0;
23+
std::vector<std::future<int>> results;
24+
std::mutex m;
25+
thread_pool->SetPoolSize(4);
26+
thread_pool->SetQueueSizeLimit(6);
27+
int round = 10;
28+
for (int i = 0; i < round; ++i) {
29+
results.emplace_back(thread_pool->GeneralEnqueue(
30+
[&data, &m](int i) -> int {
31+
std::this_thread::sleep_for(std::chrono::seconds(1));
32+
std::lock_guard lock(m);
33+
vsag::logger::info("current data:{}", data);
34+
data++;
35+
return i * i;
36+
},
37+
i));
38+
}
39+
thread_pool->WaitUntilEmpty();
40+
REQUIRE(data == round);
41+
}

tests/test_index_old.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ TEST_CASE("remove vectors from the index", "[ft][index]") {
524524
vsag::Options::Instance().logger()->SetLevel(vsag::Logger::Level::kDEBUG);
525525
int64_t num_vectors = 1000;
526526
int64_t dim = 64;
527-
auto index_name = GENERATE("fresh_hnsw", "diskann");
527+
auto index_name = GENERATE("fresh_hnsw", "diskann", "hnsw");
528528
auto metric_type = GENERATE("cosine", "ip", "l2");
529529

530530
bool need_normalize = metric_type != std::string("cosine");

0 commit comments

Comments
 (0)