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

Replace deleted elements at addition #418

Merged
merged 28 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Refactoring
  • Loading branch information
Dmitry Yashunin committed Nov 12, 2022
commit c4bedcf0001564184941e54f871825960c963d85
18 changes: 9 additions & 9 deletions hnswlib/hnswalg.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
mutable std::atomic<long> metric_distance_computations{0};
mutable std::atomic<long> metric_hops{0};

bool replace_deleted = false;
bool replace_deleted_ = false;
dyashuni marked this conversation as resolved.
Show resolved Hide resolved

std::mutex deleted_elements_lock;
std::unordered_set<tableint> deleted_elements;
Expand All @@ -82,7 +82,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
bool nmslib = false,
size_t max_elements = 0,
bool replace_deleted = false)
: replace_deleted(replace_deleted) {
: replace_deleted_(replace_deleted) {
loadIndex(location, s, max_elements);
}

Expand All @@ -97,7 +97,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
: link_list_locks_(max_elements),
link_list_update_locks_(max_update_element_locks),
element_levels_(max_elements),
replace_deleted(replace_deleted) {
replace_deleted_(replace_deleted) {
max_elements_ = max_elements;
num_deleted_ = 0;
data_size_ = s->get_data_size();
Expand Down Expand Up @@ -703,7 +703,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
for (size_t i = 0; i < cur_element_count; i++) {
if (isMarkedDeleted(i)) {
num_deleted_ += 1;
if (replace_deleted) deleted_elements.insert(i);
if (replace_deleted_) deleted_elements.insert(i);
}
}

Expand Down Expand Up @@ -757,7 +757,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
unsigned char *ll_cur = ((unsigned char *)get_linklist0(internalId))+2;
*ll_cur |= DELETE_MARK;
num_deleted_ += 1;
if (replace_deleted) deleted_elements.insert(internalId);
if (replace_deleted_) deleted_elements.insert(internalId);
} else {
throw std::runtime_error("The requested to delete element is already deleted");
}
Expand Down Expand Up @@ -786,7 +786,7 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
unsigned char *ll_cur = ((unsigned char *)get_linklist0(internalId)) + 2;
*ll_cur &= ~DELETE_MARK;
num_deleted_ -= 1;
if (replace_deleted) deleted_elements.erase(internalId);
if (replace_deleted_) deleted_elements.erase(internalId);
} else {
throw std::runtime_error("The requested to undelete element is not deleted");
}
Expand Down Expand Up @@ -817,9 +817,9 @@ class HierarchicalNSW : public AlgorithmInterface<dist_t, filter_func_t> {
*
* If deleted point was replaced returns its label, else returns label of added point
*/
labeltype insertPoint(const void* data_point, labeltype label) {
if (!replace_deleted) {
throw std::runtime_error("Don't use insertPoint when replacement of deleted elements is disabled");
labeltype addPointToVacantPlace(const void* data_point, labeltype label) {
dyashuni marked this conversation as resolved.
Show resolved Hide resolved
if (!replace_deleted_) {
throw std::runtime_error("Can't use addPointToVacantPlace when replacement of deleted elements is disabled");
}

std::unique_lock <std::mutex> tmp_del_el_lock(deleted_elements_lock);
Expand Down
16 changes: 8 additions & 8 deletions python_bindings/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Index {
}


py::object insertItems_return_numpy(py::object input, py::object ids_ = py::none(), int num_threads = -1) {
py::object add_items_to_vacant_place_return_numpy(py::object input, py::object ids_ = py::none(), int num_threads = -1) {
size_t rows, features;
hnswlib::labeltype* data_numpy_l = NULL;

Expand Down Expand Up @@ -261,7 +261,7 @@ class Index {
normalize_vector(vector_data, norm_array.data());
vector_data = norm_array.data();
}
hnswlib::labeltype label = appr_alg->insertPoint((void*)vector_data, (size_t)id);
hnswlib::labeltype label = appr_alg->addPointToVacantPlace((void*)vector_data, (size_t)id);
data_numpy_l[start] = label;
start = 1;
ep_added = true;
Expand All @@ -271,7 +271,7 @@ class Index {
if (normalize == false) {
ParallelFor(start, rows, num_threads, [&](size_t row, size_t threadId) {
size_t id = ids.size() ? ids.at(row) : (cur_l + row);
hnswlib::labeltype label = appr_alg->insertPoint((void*)items.data(row), (size_t)id);
hnswlib::labeltype label = appr_alg->addPointToVacantPlace((void*)items.data(row), (size_t)id);
data_numpy_l[row] = label;
});
}
Expand All @@ -283,7 +283,7 @@ class Index {
normalize_vector((float*)items.data(row), (norm_array.data() + start_idx));

size_t id = ids.size() ? ids.at(row) : (cur_l + row);
hnswlib::labeltype label = appr_alg->insertPoint((void*)(norm_array.data() + start_idx), (size_t)id);
hnswlib::labeltype label = appr_alg->addPointToVacantPlace((void*)(norm_array.data() + start_idx), (size_t)id);
data_numpy_l[row] = label;
});
}
Expand Down Expand Up @@ -472,7 +472,7 @@ class Index {
"ef"_a = appr_alg->ef_,
"has_deletions"_a = (bool)appr_alg->num_deleted_,
"size_links_per_element"_a = appr_alg->size_links_per_element_,
"replace_deleted"_a = appr_alg->replace_deleted,
"replace_deleted"_a = appr_alg->replace_deleted_,

"label_lookup_external"_a = py::array_t<hnswlib::labeltype>(
{ appr_alg->label_lookup_.size() }, // shape
Expand Down Expand Up @@ -639,7 +639,7 @@ class Index {
if (d.contains("replace_deleted")) {
replace_deleted = d["replace_deleted"].cast<bool>();
}
appr_alg->replace_deleted = replace_deleted;
appr_alg->replace_deleted_= replace_deleted;

appr_alg->num_deleted_ = 0;
bool has_deletions = d["has_deletions"].cast<bool>();
Expand Down Expand Up @@ -941,8 +941,8 @@ PYBIND11_PLUGIN(hnswlib) {
py::arg("data"),
py::arg("ids") = py::none(),
py::arg("num_threads") = -1)
.def("insert_items",
&Index<float>::insertItems_return_numpy,
.def("add_items_to_vacant_place",
&Index<float>::add_items_to_vacant_place_return_numpy,
py::arg("data"),
py::arg("ids") = py::none(),
py::arg("num_threads") = -1)
Expand Down
6 changes: 3 additions & 3 deletions python_bindings/tests/bindings_test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def testRandomSelf(self):
print("Inserting batch 3 by replacing deleted elements")
# Maximum number of elements is reached therefore we cannot add new items
# but we can replace the deleted ones
labels_replaced = hnsw_index.insert_items(data3, labels3)
labels_replaced = hnsw_index.add_items_to_vacant_place(data3, labels3)
labels2_deleted_list = [l[0] for l in labels2_deleted]
labels_replaced_list = labels_replaced.tolist()
labels2_deleted_list.sort()
Expand Down Expand Up @@ -114,7 +114,7 @@ def testRandomSelf(self):

# Insert batch 4
print("Inserting batch 4 by replacing deleted elements")
labels_replaced = hnsw_index.insert_items(data4, labels4)
labels_replaced = hnsw_index.add_items_to_vacant_place(data4, labels4)

# Check recall
print("Checking recall")
Expand All @@ -133,7 +133,7 @@ def testRandomSelf(self):
del hnsw_index
# Insert batch 3
print("Inserting batch 3 by replacing deleted elements")
labels_replaced = hnsw_index_pckl.insert_items(data3, labels3)
labels_replaced = hnsw_index_pckl.add_items_to_vacant_place(data3, labels3)

# Check recall
print("Checking recall")
Expand Down