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
Add stress test to check multithreading
  • Loading branch information
Dmitry Yashunin committed Nov 12, 2022
commit 0f3214c0e275827186bb7a5aa82c9e430f67f81d
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ var/
.idea/
.vscode/
.vs/
**.DS_Store
2 changes: 1 addition & 1 deletion python_bindings/tests/bindings_test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def testRandomSelf(self):
for l in labels2_deleted:
hnsw_index.mark_deleted(l[0])
labels1_found, _ = hnsw_index.knn_query(data1, k=1)
items = hnsw_index.get_items(labels1)
items = hnsw_index.get_items(labels1_found)
diff_with_gt_labels = np.mean(np.abs(data1-items))
self.assertAlmostEqual(diff_with_gt_labels, 0, delta=1e-3)

Expand Down
62 changes: 62 additions & 0 deletions python_bindings/tests/bindings_test_stress_mt_replace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import unittest

import numpy as np

import hnswlib


class RandomSelfTestCase(unittest.TestCase):
def testRandomSelf(self):
dim = 16
num_elements = 1_000
max_num_elements = 2 * num_elements

# Generating sample data
# batch 1
first_id = 0
last_id = num_elements
labels1 = np.arange(first_id, last_id)
data1 = np.float32(np.random.random((num_elements, dim)))
# batch 2
first_id += num_elements
last_id += num_elements
labels2 = np.arange(first_id, last_id)
data2 = np.float32(np.random.random((num_elements, dim)))
# batch 3
first_id += num_elements
last_id += num_elements
labels3 = np.arange(first_id, last_id)
data3 = np.float32(np.random.random((num_elements, dim)))

# Declaring index
for _ in range(100):
hnsw_index = hnswlib.Index(space='l2', dim=dim)
hnsw_index.init_index(max_elements=max_num_elements, ef_construction=200, M=16, replace_deleted=True)

hnsw_index.set_ef(100)
hnsw_index.set_num_threads(50)

# Add batch 1 and 2
hnsw_index.add_items(data1, labels1)
hnsw_index.add_items(data2, labels2) # maximum number of elements is reached

# Delete nearest neighbors of batch 2
labels2_deleted, _ = hnsw_index.knn_query(data2, k=1)
for l in labels2_deleted:
hnsw_index.mark_deleted(l[0])
labels1_found, _ = hnsw_index.knn_query(data1, k=1)
items = hnsw_index.get_items(labels1_found)
diff_with_gt_labels = np.mean(np.abs(data1-items))
self.assertAlmostEqual(diff_with_gt_labels, 0, delta=1e-3)

labels2_after, _ = hnsw_index.knn_query(data2, k=1)
labels2_after_flat = labels2_after.flatten()
labels2_deleted_flat = labels2_deleted.flatten()
common = np.intersect1d(labels2_after_flat, labels2_deleted_flat)
self.assertTrue(common.size == 0)

# Replace 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.add_items_to_vacant_place(data3, labels3)