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

feat: add pq support for hnsw searching #122

Merged
merged 24 commits into from
Aug 8, 2022
Merged

Conversation

gusye1234
Copy link
Contributor

In order to reduce the memory usage of HNSW, we can store the embedding quantized by PQ(normally will be uint8_t). Also, the distance computation will become a look-up operation, which should be a little bit faster while adding and searching.

@codecov
Copy link

codecov bot commented Jun 27, 2022

Codecov Report

Merging #122 (3ad36bb) into main (534eed8) will increase coverage by 6.02%.
The diff coverage is 98.11%.

@@            Coverage Diff             @@
##             main     #122      +/-   ##
==========================================
+ Coverage   60.13%   66.16%   +6.02%     
==========================================
  Files          23       23              
  Lines        1312     1318       +6     
==========================================
+ Hits          789      872      +83     
+ Misses        523      446      -77     
Flag Coverage Δ
annlite 66.16% <98.11%> (+6.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
annlite/core/codec/base.py 81.81% <ø> (+15.15%) ⬆️
annlite/core/index/pq_index.py 100.00% <ø> (+70.00%) ⬆️
annlite/storage/base.py 56.66% <ø> (+0.41%) ⬆️
annlite/storage/table.py 89.11% <ø> (+0.40%) ⬆️
annlite/core/index/hnsw/index.py 89.42% <97.67%> (+7.33%) ⬆️
annlite/__init__.py 100.00% <100.00%> (ø)
annlite/container.py 77.83% <100.00%> (+1.64%) ⬆️
annlite/core/codec/pq.py 86.58% <100.00%> (+12.22%) ⬆️
annlite/index.py 74.73% <100.00%> (+17.51%) ⬆️
annlite/math.py 32.14% <100.00%> (+2.51%) ⬆️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 157a1a9...3ad36bb. Read the comment docs.

annlite/core/codec/base.py Outdated Show resolved Hide resolved
annlite/core/index/hnsw/index.py Outdated Show resolved Hide resolved
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Copy link
Member

@numb3r3 numb3r3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Totally, all of the implementations are perfect. We need some minor revisions to polish the code and API signature.

annlite/container.py Outdated Show resolved Hide resolved
annlite/container.py Outdated Show resolved Hide resolved
annlite/container.py Show resolved Hide resolved
annlite/core/index/hnsw/index.py Outdated Show resolved Hide resolved
annlite/index.py Outdated Show resolved Hide resolved
annlite/index.py Outdated Show resolved Hide resolved
annlite/index.py Outdated Show resolved Hide resolved
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
@gusye1234
Copy link
Contributor Author

I'm wondering why the below if (!ep_added) branch is needed?

if (!ep_added) {
size_t id = ids.size() ? ids.at(0) : (cur_l);
float *vector_data = (float *)items.data(0);
std::vector<float> norm_array(dim);
if (normalize) {
normalize_vector(vector_data, norm_array.data());
vector_data = norm_array.data();
}
appr_alg->addPoint((void *)vector_data, (size_t)id);
start = 1;
ep_added = true;
}
py::gil_scoped_release l;
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);
appr_alg->addPoint((void *)items.data(row), (size_t)id);
});
} else {
std::vector<float> norm_array(num_threads * dim);
ParallelFor(start, rows, num_threads, [&](size_t row, size_t threadId) {
// normalize vector:
size_t start_idx = threadId * dim;
normalize_vector((float *)items.data(row),
(norm_array.data() + start_idx));
size_t id = ids.size() ? ids.at(row) : (cur_l + row);
appr_alg->addPoint((void *)(norm_array.data() + start_idx),
(size_t)id);
});
};

Seem like the adding of the first row can be merged into ParallelFor

bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
include/hnswlib/space_pq.h Show resolved Hide resolved
include/hnswlib/space_pq.h Outdated Show resolved Hide resolved
include/hnswlib/space_pq.h Show resolved Hide resolved
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
annlite/core/codec/pq.py Outdated Show resolved Hide resolved
annlite/helper.py Outdated Show resolved Hide resolved
throw py::attribute_error(
"PQ class returning the codebook with wrong dimension");
}
std::shared_ptr<float *> codebook_buffer =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::shared_ptr<float *> codebook_buffer =
std::shared_ptr<float> codebook_buffer =

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the template type is the type, u do not want a shared pointer to a float pointer, but a shared poiinter to a float.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now that I see it, I am not sure this is what u want, what u would like is to have an std::array<float> instead of a float*. Like this u can use this std::array as an object and forget about the pointers, and just pass it around as const reference

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the codebook has a known size u should use std::array to handle the ownership of the underlying buffer in a safe way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a mistake about the shared_ptr's type here, it should be float.
May I ask when you suggested std::array, what's the solution in your mind? Do you mean we should copy the codebook from the buffer?

Copy link
Member

@JoanFM JoanFM left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please stick to only one syntax to detect what variables are object members. this-> or not this-> but do not mix it

include/hnswlib/space_pq.h Outdated Show resolved Hide resolved
DISTFUNC<float> fstdistfunc_;
size_t data_size_, d_subvectors;
pq_dist_param_t param;
std::shared_ptr<float *> codebook;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shared_ptr as mentioned in other PRs is not clear. If u can use std::shared_ptr means u can use std::array and this is what u should have.

@numb3r3 numb3r3 linked an issue Jul 22, 2022 that may be closed by this pull request
1 task
annlite/core/codec/base.py Outdated Show resolved Hide resolved
annlite/core/codec/pq.py Outdated Show resolved Hide resolved
annlite/core/codec/pq.py Outdated Show resolved Hide resolved
annlite/core/index/hnsw/index.py Outdated Show resolved Hide resolved
annlite/helper.py Outdated Show resolved Hide resolved
bindings/hnsw_bindings.cpp Show resolved Hide resolved
bindings/hnsw_bindings.cpp Outdated Show resolved Hide resolved
throw py::attribute_error(
"PQ class returning the codebook with wrong dimension");
}
std::shared_ptr<float *> codebook_buffer =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the codebook has a known size u should use std::array to handle the ownership of the underlying buffer in a safe way

Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
@gusye1234
Copy link
Contributor Author

gusye1234 commented Jul 28, 2022

the above commits fixed the before problems, except for using the std::array as the param for PQ_Space.
Also, I moved the normalization for the embeddings to Python. If this is not what you want, we can move it back.

Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
@gusye1234 gusye1234 requested review from numb3r3 and JoanFM July 29, 2022 10:39
@gusye1234
Copy link
Contributor Author

I removed the ownership of codebook in PQ_Space. The computation will finish as long as the construction of PQ_Space is finished, and no need for the codebook after that. Will that work for you? @JoanFM

@gusye1234
Copy link
Contributor Author

The conflicts happened at very beginning of this pr, so merging instead of rebasing

Signed-off-by: Jianbai Ye <jianbaiye@outlook.com>
@numb3r3 numb3r3 marked this pull request as ready for review August 8, 2022 05:57
@numb3r3 numb3r3 merged commit 690098b into jina-ai:main Aug 8, 2022
@patelprateek patelprateek mentioned this pull request Oct 8, 2022
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

implement quantized hnsw algorithm
3 participants