-
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
[Opt] Expose the detail::popc
as public API
#2346
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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,127 @@ | ||
/* | ||
* Copyright (c) 2024, 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. | ||
*/ | ||
|
||
#include <common/benchmark.hpp> | ||
|
||
#include <raft/core/popc.hpp> | ||
|
||
namespace raft::bench::core { | ||
|
||
template <typename index_t> | ||
struct PopcInputs { | ||
index_t n_rows; | ||
index_t n_cols; | ||
float sparsity; | ||
}; | ||
|
||
template <typename index_t> | ||
inline auto operator<<(std::ostream& os, const PopcInputs<index_t>& params) -> std::ostream& | ||
{ | ||
os << params.n_rows << "#" << params.n_cols << "#" << params.sparsity; | ||
return os; | ||
} | ||
|
||
template <typename index_t, typename bits_t = uint32_t> | ||
struct popc_bench : public fixture { | ||
popc_bench(const PopcInputs<index_t>& p) | ||
: params(p), | ||
n_element(raft::ceildiv(params.n_rows * params.n_cols, index_t(sizeof(bits_t) * 8))), | ||
bits_d{raft::make_device_vector<bits_t, index_t>(res, n_element)}, | ||
nnz_actual_d{raft::make_device_scalar<index_t>(res, 0)} | ||
{ | ||
} | ||
|
||
index_t create_bitmap(index_t m, index_t n, float sparsity, std::vector<bits_t>& bitmap) | ||
{ | ||
index_t total = static_cast<index_t>(m * n); | ||
index_t num_ones = static_cast<index_t>((total * 1.0f) * sparsity); | ||
index_t res = num_ones; | ||
|
||
for (auto& item : bitmap) { | ||
item = static_cast<bits_t>(0); | ||
} | ||
|
||
std::random_device rd; | ||
std::mt19937 gen(rd()); | ||
std::uniform_int_distribution<index_t> dis(0, total - 1); | ||
|
||
while (num_ones > 0) { | ||
index_t index = dis(gen); | ||
|
||
bits_t& element = bitmap[index / (8 * sizeof(bits_t))]; | ||
index_t bit_position = index % (8 * sizeof(bits_t)); | ||
|
||
if (((element >> bit_position) & 1) == 0) { | ||
element |= (static_cast<index_t>(1) << bit_position); | ||
num_ones--; | ||
} | ||
} | ||
return res; | ||
} | ||
void run_benchmark(::benchmark::State& state) override | ||
{ | ||
std::ostringstream label_stream; | ||
label_stream << params; | ||
state.SetLabel(label_stream.str()); | ||
|
||
std::vector<bits_t> bits_h(n_element); | ||
auto stream = raft::resource::get_cuda_stream(res); | ||
|
||
create_bitmap(params.n_rows, params.n_cols, params.sparsity, bits_h); | ||
update_device(bits_d.data_handle(), bits_h.data(), bits_h.size(), stream); | ||
|
||
resource::sync_stream(res); | ||
|
||
loop_on_state(state, [this]() { | ||
auto bits_view = | ||
raft::make_device_vector_view<const bits_t, index_t>(bits_d.data_handle(), bits_d.size()); | ||
|
||
index_t max_len = params.n_rows * params.n_cols; | ||
auto max_len_view = raft::make_host_scalar_view<index_t>(&max_len); | ||
auto nnz_actual_view = | ||
nnz_actual_d.view(); // raft::make_device_scalar_view<index_t>(nnz_actual_d.data_handle()); | ||
raft::popc(this->handle, bits_view, max_len_view, nnz_actual_view); | ||
}); | ||
} | ||
|
||
private: | ||
raft::resources res; | ||
PopcInputs<index_t> params; | ||
index_t n_element; | ||
|
||
raft::device_vector<bits_t, index_t> bits_d; | ||
raft::device_scalar<index_t> nnz_actual_d; | ||
}; | ||
|
||
template <typename index_t> | ||
const std::vector<PopcInputs<index_t>> popc_input_vecs{ | ||
{2, 131072, 0.4}, {8, 131072, 0.5}, {16, 131072, 0.2}, {2, 8192, 0.4}, {16, 8192, 0.5}, | ||
{128, 8192, 0.2}, {1024, 8192, 0.1}, {1024, 8192, 0.1}, {1024, 8192, 0.1}, {1024, 8192, 0.1}, | ||
|
||
{1024, 8192, 0.1}, {1024, 8192, 0.1}, {1024, 8192, 0.1}, {1024, 8192, 0.1}, | ||
|
||
{1024, 8192, 0.4}, {1024, 8192, 0.5}, {1024, 8192, 0.2}, {1024, 8192, 0.4}, {1024, 8192, 0.5}, | ||
{1024, 8192, 0.2}, {1024, 8192, 0.4}, {1024, 8192, 0.5}, {1024, 8192, 0.2}, {1024, 8192, 0.4}, | ||
{1024, 8192, 0.5}, {1024, 8192, 0.2}, | ||
|
||
{1024, 8192, 0.5}, {1024, 8192, 0.2}, {1024, 8192, 0.4}, {1024, 8192, 0.5}, {1024, 8192, 0.2}, | ||
{1024, 8192, 0.4}, {1024, 8192, 0.5}, {1024, 8192, 0.2}}; | ||
|
||
using PopcBenchI64 = popc_bench<int64_t>; | ||
|
||
RAFT_BENCH_REGISTER(PopcBenchI64, "", popc_input_vecs<int64_t>); | ||
|
||
} // namespace raft::bench::core |
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
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,41 @@ | ||
/* | ||
* Copyright (c) 2024, 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/detail/popc.cuh> | ||
namespace raft { | ||
|
||
/** | ||
* @brief Count the number of bits that are set to 1 in a vector. | ||
* | ||
* @tparam value_t the value type of the vector. | ||
* @tparam index_t the index type of vector and scalar. | ||
* | ||
* @param[in] res RAFT handle for managing expensive resources | ||
* @param[in] values Device vector view containing the values to be processed. | ||
* @param[in] max_len Host scalar view to store the Maximum number of bits to count. | ||
* @param[out] counter Device scalar view to store the number of bits that are set to 1. | ||
*/ | ||
template <typename value_t, typename index_t> | ||
void popc(const raft::resources& res, | ||
device_vector_view<value_t, index_t> values, | ||
raft::host_scalar_view<index_t> max_len, | ||
raft::device_scalar_view<index_t> counter) | ||
{ | ||
detail::popc(res, values, max_len, counter); | ||
} | ||
|
||
} // namespace raft |
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
Oops, something went wrong.
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.
To be quite honest, I'm not sure this is something that belongs in
raft::core
.Also, I'm a little confused by the max_len- can you explain why this additional argument is needed? This API seems a little implementation-specific, which makes me wonder if it should be more strongly coupled to the
bitset
/bitmap
.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.
The original wrap of
popc
is here: https://github.com/rapidsai/raft/blob/branch-24.08/cpp/include/raft/core/detail/mdspan_util.cuh#L48. But it can only calc nonzero bits in one int32/int64, while this one can calculate on an integer array. Would you have some recommendations on thenamespace
? Maybe theraft::linalg
is another choice.The
max_len
should be required to indicate the max bits to be processed. For example, we have 100 bits needed to be calculated and at least 4uint32_t
s to present them(get 128 bits physically). In this situation, amax_len
or similar parameter is needed to tell thepopc
to stop on the right bits.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.
This API is mainly for clean code. The
detail::popc
includes at least 10 lines of code because it has to process the corner cases. If we were using the original code, we would have to copy that block of code anywhere, that is ugly.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.
Since this is a computational function, I don't think it belongs in
raft/core
. I question whether or not it actually belongs inraft/matrix
or inraft/utils
, though. Hmm.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.
I prefer
raft/utils
as it seems more reasonable. If you agree, I will move it there. @cjnolet