-
Notifications
You must be signed in to change notification settings - Fork 742
Stop condition #490
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
Merged
Merged
Stop condition #490
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0461fd0
Add stop condition, multivector search, epsilon search
66a2294
Fix include
3008415
Update readme
bdf7cd9
Update multivector tests
b42045f
One header file
dyashuni a2657c3
Add bare_bone_search flag
dyashuni 2d4d37c
Fix epsilon search
dyashuni 64ac045
Refactoring
dyashuni dd9bc09
Adress comments
dyashuni 107f76d
Fix assert
dyashuni 8fb4dc4
Add ef to multivector search, return vector, refactoring
dyashuni 61ce236
Refactoring
dyashuni 06f1330
Adress comments
dyashuni 87b97fe
Add bare bone search comment
dyashuni dd72bd3
Remove has_deletions flag
dyashuni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ var/ | |
| .vscode/ | ||
| .vs/ | ||
| **.DS_Store | ||
| *.pyc | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,66 @@ | ||
| #include "../../hnswlib/hnswlib.h" | ||
|
|
||
| typedef unsigned int docidtype; | ||
| typedef float dist_t; | ||
|
|
||
| int main() { | ||
| int dim = 16; // Dimension of the elements | ||
| int max_elements = 10000; // Maximum number of elements, should be known beforehand | ||
| int M = 16; // Tightly connected with internal dimensionality of the data | ||
| // strongly affects the memory consumption | ||
| int ef_construction = 200; // Controls index search speed/build speed tradeoff | ||
| int min_num_candidates = 100; // Minimum number of candidates to search in the epsilon region | ||
| // this parameter is similar to ef | ||
|
|
||
| int num_quries = 5; | ||
| float epsilon = 2.0; // Squared distance to query | ||
|
|
||
| // Initing index | ||
| hnswlib::L2Space space(dim); | ||
| hnswlib::HierarchicalNSW<dist_t>* alg_hnsw = new hnswlib::HierarchicalNSW<dist_t>(&space, max_elements, M, ef_construction); | ||
|
|
||
| // Generate random data | ||
| std::mt19937 rng; | ||
| rng.seed(47); | ||
| std::uniform_real_distribution<> distrib_real; | ||
|
|
||
| size_t data_point_size = space.get_data_size(); | ||
| char* data = new char[data_point_size * max_elements]; | ||
| for (int i = 0; i < max_elements; i++) { | ||
| char* point_data = data + i * data_point_size; | ||
| for (int j = 0; j < dim; j++) { | ||
| char* vec_data = point_data + j * sizeof(float); | ||
| float value = distrib_real(rng); | ||
| *(float*)vec_data = value; | ||
| } | ||
| } | ||
|
|
||
| // Add data to index | ||
| for (int i = 0; i < max_elements; i++) { | ||
| hnswlib::labeltype label = i; | ||
| char* point_data = data + i * data_point_size; | ||
| alg_hnsw->addPoint(point_data, label); | ||
| } | ||
|
|
||
| // Query random vectors | ||
| for (int i = 0; i < num_quries; i++) { | ||
| char* query_data = new char[data_point_size]; | ||
| for (int j = 0; j < dim; j++) { | ||
| size_t offset = j * sizeof(float); | ||
| char* vec_data = query_data + offset; | ||
| float value = distrib_real(rng); | ||
| *(float*)vec_data = value; | ||
| } | ||
| std::cout << "Query #" << i << "\n"; | ||
| hnswlib::EpsilonSearchStopCondition<dist_t> stop_condition(epsilon, min_num_candidates, max_elements); | ||
| std::vector<std::pair<float, hnswlib::labeltype>> result = | ||
| alg_hnsw->searchStopConditionClosest(query_data, stop_condition); | ||
| size_t num_vectors = result.size(); | ||
| std::cout << "Found " << num_vectors << " vectors\n"; | ||
| delete[] query_data; | ||
| } | ||
|
|
||
| delete[] data; | ||
| delete alg_hnsw; | ||
| return 0; | ||
| } | ||
This file contains hidden or 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,83 @@ | ||
| #include "../../hnswlib/hnswlib.h" | ||
|
|
||
| typedef unsigned int docidtype; | ||
| typedef float dist_t; | ||
|
|
||
| int main() { | ||
| int dim = 16; // Dimension of the elements | ||
| int max_elements = 10000; // Maximum number of elements, should be known beforehand | ||
| int M = 16; // Tightly connected with internal dimensionality of the data | ||
| // strongly affects the memory consumption | ||
| int ef_construction = 200; // Controls index search speed/build speed tradeoff | ||
|
|
||
| int num_quries = 5; | ||
dyashuni marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| int num_docs = 5; // Number of documents to search | ||
| int ef_collection = 6; // Number of candidate documents during the search | ||
| // Controlls the recall: higher ef leads to better accuracy, but slower search | ||
| docidtype min_doc_id = 0; | ||
| docidtype max_doc_id = 9; | ||
|
|
||
| // Initing index | ||
| hnswlib::MultiVectorL2Space<docidtype> space(dim); | ||
| hnswlib::HierarchicalNSW<dist_t>* alg_hnsw = new hnswlib::HierarchicalNSW<dist_t>(&space, max_elements, M, ef_construction); | ||
|
|
||
| // Generate random data | ||
| std::mt19937 rng; | ||
| rng.seed(47); | ||
| std::uniform_real_distribution<> distrib_real; | ||
| std::uniform_int_distribution<docidtype> distrib_docid(min_doc_id, max_doc_id); | ||
|
|
||
| size_t data_point_size = space.get_data_size(); | ||
| char* data = new char[data_point_size * max_elements]; | ||
| for (int i = 0; i < max_elements; i++) { | ||
| // set vector value | ||
| char* point_data = data + i * data_point_size; | ||
| for (int j = 0; j < dim; j++) { | ||
| char* vec_data = point_data + j * sizeof(float); | ||
| float value = distrib_real(rng); | ||
| *(float*)vec_data = value; | ||
| } | ||
| // set document id | ||
| docidtype doc_id = distrib_docid(rng); | ||
| space.set_doc_id(point_data, doc_id); | ||
| } | ||
|
|
||
| // Add data to index | ||
| std::unordered_map<hnswlib::labeltype, docidtype> label_docid_lookup; | ||
| for (int i = 0; i < max_elements; i++) { | ||
| hnswlib::labeltype label = i; | ||
| char* point_data = data + i * data_point_size; | ||
| alg_hnsw->addPoint(point_data, label); | ||
| label_docid_lookup[label] = space.get_doc_id(point_data); | ||
| } | ||
|
|
||
| // Query random vectors | ||
| size_t query_size = dim * sizeof(float); | ||
| for (int i = 0; i < num_quries; i++) { | ||
| char* query_data = new char[query_size]; | ||
| for (int j = 0; j < dim; j++) { | ||
| size_t offset = j * sizeof(float); | ||
| char* vec_data = query_data + offset; | ||
| float value = distrib_real(rng); | ||
| *(float*)vec_data = value; | ||
| } | ||
| std::cout << "Query #" << i << "\n"; | ||
| hnswlib::MultiVectorSearchStopCondition<docidtype, dist_t> stop_condition(space, num_docs, ef_collection); | ||
| std::vector<std::pair<float, hnswlib::labeltype>> result = | ||
| alg_hnsw->searchStopConditionClosest(query_data, stop_condition); | ||
| size_t num_vectors = result.size(); | ||
|
|
||
| std::unordered_map<docidtype, size_t> doc_counter; | ||
| for (auto pair: result) { | ||
| hnswlib::labeltype label = pair.second; | ||
| docidtype doc_id = label_docid_lookup[label]; | ||
| doc_counter[doc_id] += 1; | ||
| } | ||
| std::cout << "Found " << doc_counter.size() << " documents, " << num_vectors << " vectors\n"; | ||
| delete[] query_data; | ||
| } | ||
|
|
||
| delete[] data; | ||
| delete alg_hnsw; | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.