-
Notifications
You must be signed in to change notification settings - Fork 197
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
[FEA] Codepacking for IVF-flat #1632
Merged
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
cc9cbd3
Unpack list data kernel
tarang-jain 28484ef
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain e39ee56
update packing and unpacking functions
tarang-jain 68bf927
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 78d6380
Update codepacker
tarang-jain 49a8834
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 897338e
refactor codepacker (does not build)
tarang-jain c1d80f5
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 2a2ee51
Undo deletions
tarang-jain 834dd2c
undo yaml changes
tarang-jain 6013429
style
tarang-jain ab6345a
Update tests, correct make_list_extents
tarang-jain ed80d1a
More changes
tarang-jain cdff9e1
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 7412272
debugging
tarang-jain 700ea82
Working build
tarang-jain 27451c6
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 9d742ef
rename codepacking api
tarang-jain d1ef8a1
Updated gtest
tarang-jain e187147
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 4f233a6
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 4ee99e3
updates
tarang-jain 22f4f80
update testing
tarang-jain 9f4e22c
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain c95d1e0
updates
tarang-jain da78c66
Update testing, pow2
tarang-jain 5cc6dc9
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 15db0c6
remove unneccessary changes
tarang-jain 154dc6d
Delete log.txt
tarang-jain 47d6421
updates
tarang-jain 0f1d106
Merge branch 'faiss-ivf' of https://github.com/tarang-jain/raft into …
tarang-jain e2e1308
ore cleanup
tarang-jain 3f470c8
Merge branch 'branch-23.08' of https://github.com/rapidsai/raft into …
tarang-jain 41a49b2
style
tarang-jain 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 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 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,115 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <raft/core/device_mdspan.hpp> | ||
#include <raft/core/resource/cuda_stream.hpp> | ||
#include <raft/core/resources.hpp> | ||
#include <raft/neighbors/ivf_flat_types.hpp> | ||
|
||
#ifdef _RAFT_HAS_CUDA | ||
#include <raft/util/pow2_utils.cuh> | ||
#else | ||
#include <raft/util/integer_utils.hpp> | ||
#endif | ||
|
||
namespace raft::neighbors::ivf_flat::codepacker { | ||
|
||
template <typename T> | ||
_RAFT_HOST_DEVICE inline auto roundDown(T x) | ||
{ | ||
#if defined(_RAFT_HAS_CUDA) | ||
return Pow2<kIndexGroupSize>::roundDown(x); | ||
#else | ||
return raft::round_down_safe(x, kIndexGroupSize); | ||
#endif | ||
} | ||
|
||
template <typename T> | ||
_RAFT_HOST_DEVICE inline auto mod(T x) | ||
{ | ||
#if defined(_RAFT_HAS_CUDA) | ||
return Pow2<kIndexGroupSize>::mod(x); | ||
#else | ||
return x % kIndexGroupSize; | ||
#endif | ||
} | ||
|
||
/** | ||
* Write one flat code into a block by the given offset. The offset indicates the id of the record | ||
* in the list. This function interleaves the code and is intended to later copy the interleaved | ||
* codes over to the IVF list on device. NB: no memory allocation happens here; the block must fit | ||
* the record (offset + 1). | ||
* | ||
* @tparam T | ||
* | ||
* @param[in] flat_code input flat code | ||
* @param[out] block block of memory to write interleaved codes to | ||
* @param[in] dim dimension of the flat code | ||
* @param[in] veclen size of interleaved data chunks | ||
* @param[in] offset how many records to skip before writing the data into the list | ||
*/ | ||
template <typename T> | ||
_RAFT_HOST_DEVICE void pack_1( | ||
const T* flat_code, T* block, uint32_t dim, uint32_t veclen, uint32_t offset) | ||
{ | ||
// The data is written in interleaved groups of `index::kGroupSize` vectors | ||
// using interleaved_group = Pow2<kIndexGroupSize>; | ||
|
||
// Interleave dimensions of the source vector while recording it. | ||
// NB: such `veclen` is selected, that `dim % veclen == 0` | ||
auto group_offset = roundDown(offset); | ||
auto ingroup_id = mod(offset) * veclen; | ||
|
||
for (uint32_t l = 0; l < dim; l += veclen) { | ||
for (uint32_t j = 0; j < veclen; j++) { | ||
block[group_offset * dim + l * kIndexGroupSize + ingroup_id + j] = flat_code[l + j]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Unpack 1 record of a single list (cluster) in the index to fetch the flat code. The offset | ||
* indicates the id of the record. This function fetches one flat code from an interleaved code. | ||
* | ||
* @tparam T | ||
* | ||
* @param[in] block interleaved block. The block can be thought of as the whole inverted list in | ||
* interleaved format. | ||
* @param[out] flat_code output flat code | ||
* @param[in] dim dimension of the flat code | ||
* @param[in] veclen size of interleaved data chunks | ||
* @param[in] offset fetch the flat code by the given offset | ||
*/ | ||
template <typename T> | ||
_RAFT_HOST_DEVICE void unpack_1( | ||
const T* block, T* flat_code, uint32_t dim, uint32_t veclen, uint32_t offset) | ||
{ | ||
// The data is written in interleaved groups of `index::kGroupSize` vectors | ||
// using interleaved_group = Pow2<kIndexGroupSize>; | ||
|
||
// NB: such `veclen` is selected, that `dim % veclen == 0` | ||
auto group_offset = roundDown(offset); | ||
auto ingroup_id = mod(offset) * veclen; | ||
|
||
for (uint32_t l = 0; l < dim; l += veclen) { | ||
for (uint32_t j = 0; j < veclen; j++) { | ||
flat_code[l + j] = block[group_offset * dim + l * kIndexGroupSize + ingroup_id + j]; | ||
} | ||
} | ||
} | ||
} // namespace raft::neighbors::ivf_flat::codepacker |
This file contains 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,110 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <raft/neighbors/detail/ivf_flat_build.cuh> | ||
#include <raft/neighbors/ivf_flat_types.hpp> | ||
|
||
#include <raft/core/device_mdspan.hpp> | ||
#include <raft/core/resources.hpp> | ||
|
||
namespace raft::neighbors::ivf_flat::helpers { | ||
/** | ||
* @defgroup ivf_flat_helpers Helper functions for manipulationg IVF Flat Index | ||
* @{ | ||
*/ | ||
|
||
namespace codepacker { | ||
|
||
/** | ||
* Write flat codes into an existing list by the given offset. | ||
* | ||
* NB: no memory allocation happens here; the list must fit the data (offset + n_vec). | ||
* | ||
* Usage example: | ||
* @code{.cpp} | ||
* auto list_data = index.lists()[label]->data.view(); | ||
* // allocate the buffer for the input codes | ||
* auto codes = raft::make_device_matrix<T>(res, n_vec, index.dim()); | ||
* ... prepare n_vecs to pack into the list in codes ... | ||
* // write codes into the list starting from the 42nd position | ||
* ivf_pq::helpers::codepacker::pack( | ||
* res, make_const_mdspan(codes.view()), index.veclen(), 42, list_data); | ||
* @endcode | ||
* | ||
* @tparam T | ||
* @tparam IdxT | ||
* | ||
* @param[in] res | ||
* @param[in] codes flat codes [n_vec, dim] | ||
* @param[in] veclen size of interleaved data chunks | ||
* @param[in] offset how many records to skip before writing the data into the list | ||
* @param[inout] list_data block to write into | ||
*/ | ||
template <typename T, typename IdxT> | ||
void pack( | ||
raft::resources const& res, | ||
device_matrix_view<const T, uint32_t, row_major> codes, | ||
uint32_t veclen, | ||
uint32_t offset, | ||
device_mdspan<T, typename list_spec<uint32_t, T, IdxT>::list_extents, row_major> list_data) | ||
{ | ||
raft::neighbors::ivf_flat::detail::pack_list_data<T, IdxT>(res, codes, veclen, offset, list_data); | ||
} | ||
|
||
/** | ||
* @brief Unpack `n_take` consecutive records of a single list (cluster) in the compressed index | ||
* starting at given `offset`. | ||
* | ||
* Usage example: | ||
* @code{.cpp} | ||
* auto list_data = index.lists()[label]->data.view(); | ||
* // allocate the buffer for the output | ||
* uint32_t n_take = 4; | ||
* auto codes = raft::make_device_matrix<T>(res, n_take, index.dim()); | ||
* uint32_t offset = 0; | ||
* // unpack n_take elements from the list | ||
* ivf_pq::helpers::codepacker::unpack(res, list_data, index.veclen(), offset, codes.view()); | ||
* @endcode | ||
* | ||
* @tparam T | ||
* @tparam IdxT | ||
* | ||
* @param[in] res raft resource | ||
* @param[in] list_data block to read from | ||
* @param[in] veclen size of interleaved data chunks | ||
* @param[in] offset | ||
* How many records in the list to skip. | ||
* @param[inout] codes | ||
* the destination buffer [n_take, index.dim()]. | ||
* The length `n_take` defines how many records to unpack, | ||
* it must be <= the list size. | ||
*/ | ||
template <typename T, typename IdxT> | ||
void unpack( | ||
raft::resources const& res, | ||
device_mdspan<const T, typename list_spec<uint32_t, T, IdxT>::list_extents, row_major> list_data, | ||
uint32_t veclen, | ||
uint32_t offset, | ||
device_matrix_view<T, uint32_t, row_major> codes) | ||
{ | ||
raft::neighbors::ivf_flat::detail::unpack_list_data<T, IdxT>( | ||
res, list_data, veclen, offset, codes); | ||
} | ||
} // namespace codepacker | ||
/** @} */ | ||
} // namespace raft::neighbors::ivf_flat::helpers |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we couldn't just use a map() or map_offset() for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is no particular reason. this can indeed be put inside a map_offset.