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

Merge 0.7.0 into master #436

Merged
merged 57 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
48bbed6
add throw statement to errors in BruteforceSearch
yoshoku Mar 21, 2022
5f074d5
remove unnecessary new operators
yoshoku Mar 21, 2022
eae971a
Merge pull request #375 from yoshoku/fix_errors
yurymalkov Mar 22, 2022
d51c312
chore(ALGO_PARAMS.md): Fix typo
PLNech May 2, 2022
492e15e
Highlight code
korzhenevski Jun 2, 2022
fb3a699
fix global linkage
MasterAler Jun 8, 2022
fb1885b
Merge pull request #380 from korzhenevski/patch-1
yurymalkov Jun 14, 2022
1431632
Merge pull request #383 from MasterAler/develop
yurymalkov Jun 16, 2022
632da8f
add missing quote
jlmelville Jul 30, 2022
d3197c5
initialize fields in constructor
jlmelville Jul 30, 2022
296b687
Merge pull request #395 from jlmelville/doc/py-quote-readme
yurymalkov Aug 2, 2022
25c7383
direct member initialize fields
jlmelville Aug 4, 2022
fdb1632
Merge pull request #396 from jlmelville/bug/ctor-init
yurymalkov Aug 5, 2022
406731d
Add rust implementation
jianshu93 Aug 9, 2022
765c4ab
Filter elements with an optional filtering function.
kishorenc Aug 15, 2022
ad3440c
Filter function should be sent the label and not the internal ID.
kishorenc Aug 19, 2022
4f6dcc3
Ensure that results are not empty when reading from top results.
kishorenc Aug 19, 2022
0fc42b0
Merge pull request #401 from jianshu93/patch-1
yurymalkov Aug 21, 2022
c5be3f5
Update port git_tester.py on Windows
dyashuni Aug 21, 2022
1c833a7
Make allowAllIds static.
kishorenc Aug 25, 2022
aaee13a
Use functor for filtering.
kishorenc Aug 26, 2022
b87f623
Explicitly check for filter functor being default.
kishorenc Aug 27, 2022
e8da5a0
Refactoring
dyashuni Aug 27, 2022
74bf4a3
Add cpp tests to CI
dyashuni Aug 27, 2022
e97b37c
Merge pull request #406 from dyashuni/add_tests_to_ci
yurymalkov Aug 28, 2022
bdd0220
Use shutil
dyashuni Aug 28, 2022
f0dedf3
Remove duplicate assignment.
kishorenc Aug 28, 2022
de22860
Merge branch 'develop' into filter-elements
kishorenc Aug 28, 2022
e4705fd
Add search with filter test to CI.
kishorenc Aug 28, 2022
7f419ea
Remove constexpr for functor in test.
kishorenc Aug 28, 2022
f7d3366
USE_SSE with msvc compilers
alxvth Aug 29, 2022
23f5351
Remove inclusion of cpu_x86.h
alxvth Aug 29, 2022
e8b3e44
Add cpp tests for Windows in CI
dyashuni Aug 28, 2022
6fa8cd0
Merge pull request #404 from dyashuni/windows_tester
dyashuni Sep 2, 2022
a3e399a
Merge pull request #409 from dyashuni/cpp_windows_tests
dyashuni Sep 2, 2022
1fe7baf
Add check for is_filter_disabled.
kishorenc Sep 6, 2022
c9897b0
Add assert header.
kishorenc Sep 6, 2022
5c14e05
Merge pull request #402 from typesense/filter-elements
dyashuni Sep 6, 2022
c481fd4
Merge branch 'develop' into fix/cpu_x86_include
dyashuni Sep 6, 2022
1f49ffe
Merge pull request #408 from alxvth/fix/cpu_x86_include
dyashuni Sep 6, 2022
6d28ec0
Refactoring (#410)
dyashuni Sep 18, 2022
4ab1d61
Remove some code duplication in bindings (#416)
dyashuni Sep 20, 2022
687ca85
Update python recall test (#415)
dyashuni Sep 20, 2022
983cea9
Python: filter elements with an optional filtering function (#417)
gtsoukas Nov 9, 2022
3e006ea
Replace deleted elements at addition (#418)
dyashuni Jan 12, 2023
28681fc
Getters for max elements, element count and num deleted. (#431)
kishorenc Jan 14, 2023
978f713
Fix insufficient results during filtering. (#430)
kishorenc Jan 14, 2023
d86f8f9
Refactoring of project structure (#432)
dyashuni Jan 15, 2023
225b519
Add warning that python filter works slow in multi-threaded mode
Jan 14, 2023
32f4b02
Add comments with warnings that filter works slow in python in multit…
Jan 15, 2023
2175362
Merge pull request #433 from dyashuni/filter_warning
yurymalkov Jan 15, 2023
2c6f244
Merge pull request #379 from PLNech/patch-1
yurymalkov Jan 15, 2023
dd266bc
preliminary release notes
Jan 15, 2023
d35f428
Add construction speed logging
Jan 17, 2023
68a3387
fix a misprint
yurymalkov Jan 18, 2023
488ab52
Add cpp examples (#435)
dyashuni Jan 30, 2023
dd1bdb7
Merge pull request #434 from nmslib/v07release
yurymalkov Jan 30, 2023
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
50 changes: 46 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ name: HNSW CI
on: [push, pull_request]

jobs:
test:
test_python:
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
python-version: ['3.6', '3.7', '3.8', '3.9']
python-version: ["3.6", "3.7", "3.8", "3.9", "3.10"]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -20,3 +20,45 @@ jobs:

- name: Test
run: python -m unittest discover --start-directory python_bindings/tests --pattern "*_test*.py"

test_cpp:
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: "3.10"

- name: Build
run: |
mkdir build
cd build
cmake ..
if [ "$RUNNER_OS" == "Linux" ]; then
make
elif [ "$RUNNER_OS" == "Windows" ]; then
cmake --build ./ --config Release
fi
shell: bash

- name: Prepare test data
run: |
pip install numpy
cd examples
python update_gen_data.py
shell: bash

- name: Test
run: |
cd build
if [ "$RUNNER_OS" == "Windows" ]; then
cp ./Release/* ./
fi
./searchKnnCloserFirst_test
./searchKnnWithFilter_test
./test_updates
./test_updates update
shell: bash
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
add_executable(searchKnnCloserFirst_test examples/searchKnnCloserFirst_test.cpp)
target_link_libraries(searchKnnCloserFirst_test hnswlib)

add_executable(searchKnnWithFilter_test examples/searchKnnWithFilter_test.cpp)
target_link_libraries(searchKnnWithFilter_test hnswlib)

add_executable(main main.cpp sift_1b.cpp)
target_link_libraries(main hnswlib)
endif()
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ Please make pull requests against the `develop` branch.

When making changes please run tests (and please add a test to `python_bindings/tests` in case there is new functionality):
```bash
python -m unittest discover --start-directory python_bindings/tests --pattern "*_test*.py
python -m unittest discover --start-directory python_bindings/tests --pattern "*_test*.py"
```


Expand All @@ -267,7 +267,7 @@ https://github.com/dbaranchuk/ivf-hnsw
* Java bindings using Java Native Access: https://github.com/stepstone-tech/hnswlib-jna
* .Net implementation: https://github.com/microsoft/HNSW.Net
* CUDA implementation: https://github.com/js1010/cuhnsw

* Rust implementation for memory and thread safety purposes and There is A Trait to enable the user to implement its own distances. It takes as data slices of types T satisfying T:Serialize+Clone+Send+Sync.: https://github.com/jean-pierreBoth/hnswlib-rs

### 200M SIFT test reproduction
To download and extract the bigann dataset (from root directory):
Expand Down
2 changes: 1 addition & 1 deletion TESTING_RECALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ max_elements defines the maximum number of elements that can be stored in the st

### measuring recall example

```
```python
import hnswlib
import numpy as np

Expand Down
60 changes: 33 additions & 27 deletions examples/git_tester.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
import os
import shutil

from sys import platform
from pydriller import Repository
import os
import datetime
os.system("cp examples/speedtest.py examples/speedtest2.py") # the file has to be outside of git
for idx, commit in enumerate(Repository('.', from_tag="v0.6.0").traverse_commits()):
name=commit.msg.replace('\n', ' ').replace('\r', ' ')
print(idx, commit.hash, name)


speedtest_src_path = os.path.join("examples", "speedtest.py")
speedtest_copy_path = os.path.join("examples", "speedtest2.py")
shutil.copyfile(speedtest_src_path, speedtest_copy_path) # the file has to be outside of git

commits = list(Repository('.', from_tag="v0.6.0").traverse_commits())
print("Found commits:")
for idx, commit in enumerate(commits):
name = commit.msg.replace('\n', ' ').replace('\r', ' ')
print(idx, commit.hash, name)

for commit in Repository('.', from_tag="v0.6.0").traverse_commits():

name=commit.msg.replace('\n', ' ').replace('\r', ' ')
print(commit.hash, name)

os.system(f"git checkout {commit.hash}; rm -rf build; ")
for commit in commits:
name = commit.msg.replace('\n', ' ').replace('\r', ' ')
print("\nProcessing", commit.hash, name)

if os.path.exists("build"):
shutil.rmtree("build")
os.system(f"git checkout {commit.hash}")
print("\n\n--------------------\n\n")
ret=os.system("python -m pip install .")
print(ret)

if ret != 0:
print ("build failed!!!!")
print ("build failed!!!!")
print ("build failed!!!!")
print ("build failed!!!!")
continue

os.system(f'python examples/speedtest2.py -n "{name}" -d 4 -t 1')
os.system(f'python examples/speedtest2.py -n "{name}" -d 64 -t 1')
os.system(f'python examples/speedtest2.py -n "{name}" -d 128 -t 1')
os.system(f'python examples/speedtest2.py -n "{name}" -d 4 -t 24')
os.system(f'python examples/speedtest2.py -n "{name}" -d 128 -t 24')
ret = os.system("python -m pip install .")
print("Install result:", ret)

if ret != 0:
print("build failed!!!!")
print("build failed!!!!")
print("build failed!!!!")
print("build failed!!!!")
continue

os.system(f'python {speedtest_copy_path} -n "{name}" -d 4 -t 1')
os.system(f'python {speedtest_copy_path} -n "{name}" -d 64 -t 1')
os.system(f'python {speedtest_copy_path} -n "{name}" -d 128 -t 1')
os.system(f'python {speedtest_copy_path} -n "{name}" -d 4 -t 24')
os.system(f'python {speedtest_copy_path} -n "{name}" -d 128 -t 24')
173 changes: 173 additions & 0 deletions examples/searchKnnWithFilter_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
// This is a test file for testing the filtering feature

#include "../hnswlib/hnswlib.h"

#include <assert.h>

#include <vector>
#include <iostream>

namespace
{

using idx_t = hnswlib::labeltype;

bool pickIdsDivisibleByThree(unsigned int label_id) {
return label_id % 3 == 0;
}

bool pickIdsDivisibleBySeven(unsigned int label_id) {
return label_id % 7 == 0;
}

bool pickNothing(unsigned int label_id) {
return false;
}

template<typename filter_func_t>
void test_some_filtering(filter_func_t& filter_func, size_t div_num, size_t label_id_start) {
int d = 4;
idx_t n = 100;
idx_t nq = 10;
size_t k = 10;

std::vector<float> data(n * d);
std::vector<float> query(nq * d);

std::mt19937 rng;
rng.seed(47);
std::uniform_real_distribution<> distrib;

for (idx_t i = 0; i < n * d; ++i) {
data[i] = distrib(rng);
}
for (idx_t i = 0; i < nq * d; ++i) {
query[i] = distrib(rng);
}

hnswlib::L2Space space(d);
hnswlib::AlgorithmInterface<float,filter_func_t>* alg_brute = new hnswlib::BruteforceSearch<float,filter_func_t>(&space, 2 * n);
hnswlib::AlgorithmInterface<float,filter_func_t>* alg_hnsw = new hnswlib::HierarchicalNSW<float,filter_func_t>(&space, 2 * n);

for (size_t i = 0; i < n; ++i) {
// `label_id_start` is used to ensure that the returned IDs are labels and not internal IDs
alg_brute->addPoint(data.data() + d * i, label_id_start + i);
alg_hnsw->addPoint(data.data() + d * i, label_id_start + i);
}

// test searchKnnCloserFirst of BruteforceSearch with filtering
for (size_t j = 0; j < nq; ++j) {
const void* p = query.data() + j * d;
auto gd = alg_brute->searchKnn(p, k, filter_func);
auto res = alg_brute->searchKnnCloserFirst(p, k, filter_func);
assert(gd.size() == res.size());
size_t t = gd.size();
while (!gd.empty()) {
assert(gd.top() == res[--t]);
assert((gd.top().second % div_num) == 0);
gd.pop();
}
}

// test searchKnnCloserFirst of hnsw with filtering
for (size_t j = 0; j < nq; ++j) {
const void* p = query.data() + j * d;
auto gd = alg_hnsw->searchKnn(p, k, filter_func);
auto res = alg_hnsw->searchKnnCloserFirst(p, k, filter_func);
assert(gd.size() == res.size());
size_t t = gd.size();
while (!gd.empty()) {
assert(gd.top() == res[--t]);
assert((gd.top().second % div_num) == 0);
gd.pop();
}
}

delete alg_brute;
delete alg_hnsw;
}

template<typename filter_func_t>
void test_none_filtering(filter_func_t& filter_func, size_t label_id_start) {
int d = 4;
idx_t n = 100;
idx_t nq = 10;
size_t k = 10;

std::vector<float> data(n * d);
std::vector<float> query(nq * d);

std::mt19937 rng;
rng.seed(47);
std::uniform_real_distribution<> distrib;

for (idx_t i = 0; i < n * d; ++i) {
data[i] = distrib(rng);
}
for (idx_t i = 0; i < nq * d; ++i) {
query[i] = distrib(rng);
}

hnswlib::L2Space space(d);
hnswlib::AlgorithmInterface<float,filter_func_t>* alg_brute = new hnswlib::BruteforceSearch<float,filter_func_t>(&space, 2 * n);
hnswlib::AlgorithmInterface<float,filter_func_t>* alg_hnsw = new hnswlib::HierarchicalNSW<float,filter_func_t>(&space, 2 * n);

for (size_t i = 0; i < n; ++i) {
// `label_id_start` is used to ensure that the returned IDs are labels and not internal IDs
alg_brute->addPoint(data.data() + d * i, label_id_start + i);
alg_hnsw->addPoint(data.data() + d * i, label_id_start + i);
}

// test searchKnnCloserFirst of BruteforceSearch with filtering
for (size_t j = 0; j < nq; ++j) {
const void* p = query.data() + j * d;
auto gd = alg_brute->searchKnn(p, k, filter_func);
auto res = alg_brute->searchKnnCloserFirst(p, k, filter_func);
assert(gd.size() == res.size());
assert(0 == gd.size());
}

// test searchKnnCloserFirst of hnsw with filtering
for (size_t j = 0; j < nq; ++j) {
const void* p = query.data() + j * d;
auto gd = alg_hnsw->searchKnn(p, k, filter_func);
auto res = alg_hnsw->searchKnnCloserFirst(p, k, filter_func);
assert(gd.size() == res.size());
assert(0 == gd.size());
}

delete alg_brute;
delete alg_hnsw;
}

} // namespace

class CustomFilterFunctor: public hnswlib::FilterFunctor {
std::unordered_set<unsigned int> allowed_values;

public:
explicit CustomFilterFunctor(const std::unordered_set<unsigned int>& values) : allowed_values(values) {}

bool operator()(unsigned int id) {
return allowed_values.count(id) != 0;
}
};

int main() {
std::cout << "Testing ..." << std::endl;

// some of the elements are filtered
test_some_filtering(pickIdsDivisibleByThree, 3, 17);
test_some_filtering(pickIdsDivisibleBySeven, 7, 17);

// all of the elements are filtered
test_none_filtering(pickNothing, 17);

// functor style which can capture context
CustomFilterFunctor pickIdsDivisibleByThirteen({26, 39, 52, 65});
test_some_filtering(pickIdsDivisibleByThirteen, 13, 21);

std::cout << "Test ok" << std::endl;

return 0;
}
Loading