-
Notifications
You must be signed in to change notification settings - Fork 187
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 repeat
, sparsity
, eval_n_elements
APIs to bitset
#2439
Open
rhdong
wants to merge
1
commit into
rapidsai:branch-24.10
Choose a base branch
from
rhdong:cagra-bf
base: branch-24.10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2023, NVIDIA CORPORATION. | ||
* Copyright (c) 2023-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. | ||
|
@@ -32,12 +32,13 @@ struct test_spec_bitset { | |
uint64_t bitset_len; | ||
uint64_t mask_len; | ||
uint64_t query_len; | ||
uint64_t repeat_times; | ||
}; | ||
|
||
auto operator<<(std::ostream& os, const test_spec_bitset& ss) -> std::ostream& | ||
{ | ||
os << "bitset{bitset_len: " << ss.bitset_len << ", mask_len: " << ss.mask_len | ||
<< ", query_len: " << ss.query_len << "}"; | ||
<< ", query_len: " << ss.query_len << ", repeat_times: " << ss.repeat_times << "}"; | ||
return os; | ||
} | ||
|
||
|
@@ -80,20 +81,68 @@ void flip_cpu_bitset(std::vector<bitset_t>& bitset) | |
} | ||
} | ||
|
||
template <typename bitset_t> | ||
void repeat_cpu_bitset(std::vector<bitset_t>& input, | ||
size_t input_bits, | ||
size_t repeat, | ||
std::vector<bitset_t>& output) | ||
{ | ||
const size_t output_bits = input_bits * repeat; | ||
const size_t output_units = (output_bits + sizeof(bitset_t) * 8 - 1) / (sizeof(bitset_t) * 8); | ||
|
||
std::memset(output.data(), 0, output_units * sizeof(bitset_t)); | ||
|
||
size_t output_bit_index = 0; | ||
|
||
for (size_t r = 0; r < repeat; ++r) { | ||
for (size_t i = 0; i < input_bits; ++i) { | ||
size_t input_unit_index = i / (sizeof(bitset_t) * 8); | ||
size_t input_bit_offset = i % (sizeof(bitset_t) * 8); | ||
bool bit = (input[input_unit_index] >> input_bit_offset) & 1; | ||
|
||
size_t output_unit_index = output_bit_index / (sizeof(bitset_t) * 8); | ||
size_t output_bit_offset = output_bit_index % (sizeof(bitset_t) * 8); | ||
|
||
output[output_unit_index] |= (static_cast<bitset_t>(bit) << output_bit_offset); | ||
|
||
++output_bit_index; | ||
} | ||
} | ||
} | ||
|
||
template <typename bitset_t> | ||
double sparsity_cpu_bitset(std::vector<bitset_t>& data, size_t total_bits) | ||
{ | ||
size_t one_count = 0; | ||
for (size_t i = 0; i < total_bits; ++i) { | ||
size_t unit_index = i / (sizeof(bitset_t) * 8); | ||
size_t bit_offset = i % (sizeof(bitset_t) * 8); | ||
bool bit = (data[unit_index] >> bit_offset) & 1; | ||
if (bit == 1) { ++one_count; } | ||
} | ||
return static_cast<double>((total_bits - one_count) / (1.0 * total_bits)); | ||
} | ||
|
||
template <typename bitset_t, typename index_t> | ||
class BitsetTest : public testing::TestWithParam<test_spec_bitset> { | ||
protected: | ||
index_t static constexpr const bitset_element_size = sizeof(bitset_t) * 8; | ||
const test_spec_bitset spec; | ||
std::vector<bitset_t> bitset_result; | ||
std::vector<bitset_t> bitset_ref; | ||
std::vector<bitset_t> bitset_repeat_ref; | ||
std::vector<bitset_t> bitset_repeat_result; | ||
raft::resources res; | ||
|
||
public: | ||
explicit BitsetTest() | ||
: spec(testing::TestWithParam<test_spec_bitset>::GetParam()), | ||
bitset_result(raft::ceildiv(spec.bitset_len, uint64_t(bitset_element_size))), | ||
bitset_ref(raft::ceildiv(spec.bitset_len, uint64_t(bitset_element_size))) | ||
bitset_ref(raft::ceildiv(spec.bitset_len, uint64_t(bitset_element_size))), | ||
bitset_repeat_ref( | ||
raft::ceildiv(spec.bitset_len * spec.repeat_times, uint64_t(bitset_element_size))), | ||
bitset_repeat_result( | ||
raft::ceildiv(spec.bitset_len * spec.repeat_times, uint64_t(bitset_element_size))) | ||
{ | ||
} | ||
|
||
|
@@ -145,6 +194,37 @@ class BitsetTest : public testing::TestWithParam<test_spec_bitset> { | |
resource::sync_stream(res, stream); | ||
ASSERT_TRUE(hostVecMatch(bitset_ref, bitset_result, raft::Compare<bitset_t>())); | ||
|
||
// test sparsity, repeat and eval_n_elements | ||
if constexpr (std::is_same_v<bitset_t, uint32_t> || std::is_same_v<bitset_t, uint64_t>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why restrict the test to those data types? |
||
auto my_bitset_view = my_bitset.view(); | ||
auto sparsity_result = my_bitset_view.sparsity(res); | ||
auto sparsity_ref = sparsity_cpu_bitset(bitset_ref, size_t(spec.bitset_len)); | ||
ASSERT_EQ(sparsity_result, sparsity_ref); | ||
|
||
auto eval_n_elements = | ||
bitset_view<bitset_t, index_t>::eval_n_elements(spec.bitset_len * spec.repeat_times); | ||
ASSERT_EQ(bitset_repeat_ref.size(), eval_n_elements); | ||
|
||
auto repeat_device = raft::make_device_vector<bitset_t, index_t>(res, eval_n_elements); | ||
RAFT_CUDA_TRY(cudaMemsetAsync( | ||
repeat_device.data_handle(), 0, eval_n_elements * sizeof(bitset_t), stream)); | ||
repeat_cpu_bitset( | ||
bitset_ref, size_t(spec.bitset_len), size_t(spec.repeat_times), bitset_repeat_ref); | ||
|
||
my_bitset_view.repeat(res, index_t(spec.repeat_times), repeat_device.data_handle()); | ||
|
||
ASSERT_EQ(bitset_repeat_ref.size(), repeat_device.size()); | ||
update_host( | ||
bitset_repeat_result.data(), repeat_device.data_handle(), repeat_device.size(), stream); | ||
ASSERT_EQ(bitset_repeat_ref.size(), bitset_repeat_result.size()); | ||
ASSERT_TRUE(hostVecMatch(bitset_repeat_ref, bitset_repeat_result, raft::Compare<bitset_t>())); | ||
|
||
// recheck the sparsity after repeat | ||
sparsity_result = | ||
sparsity_cpu_bitset(bitset_repeat_result, size_t(spec.bitset_len * spec.repeat_times)); | ||
ASSERT_EQ(sparsity_result, sparsity_ref); | ||
} | ||
|
||
// Flip the bitset and re-test | ||
auto bitset_count = my_bitset.count(res); | ||
my_bitset.flip(res); | ||
|
@@ -167,13 +247,15 @@ class BitsetTest : public testing::TestWithParam<test_spec_bitset> { | |
} | ||
}; | ||
|
||
auto inputs_bitset = ::testing::Values(test_spec_bitset{32, 5, 10}, | ||
test_spec_bitset{100, 30, 10}, | ||
test_spec_bitset{1024, 55, 100}, | ||
test_spec_bitset{10000, 1000, 1000}, | ||
test_spec_bitset{1 << 15, 1 << 3, 1 << 12}, | ||
test_spec_bitset{1 << 15, 1 << 24, 1 << 13}, | ||
test_spec_bitset{1 << 25, 1 << 23, 1 << 14}); | ||
auto inputs_bitset = ::testing::Values(test_spec_bitset{32, 5, 10, 101}, | ||
test_spec_bitset{100, 30, 10, 13}, | ||
test_spec_bitset{1024, 55, 100, 1}, | ||
test_spec_bitset{10000, 1000, 1000, 100}, | ||
test_spec_bitset{1 << 15, 1 << 3, 1 << 12, 5}, | ||
test_spec_bitset{1 << 15, 1 << 24, 1 << 13, 3}, | ||
test_spec_bitset{1 << 25, 1 << 23, 1 << 14, 3}, | ||
test_spec_bitset{1 << 25, 1 << 23, 1 << 14, 201}, | ||
test_spec_bitset{10000000, 1 << 23, 1 << 14, 401}); | ||
|
||
using Uint16_32 = BitsetTest<uint16_t, uint32_t>; | ||
TEST_P(Uint16_32, Run) { run(); } | ||
|
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.
The
count()
method can also be implemented inbitset_view
, it will be useful for users.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 am also noticing that if the number of bits is not a multiple of the bitset element size the result might be wrong.
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.
Should not, because the size is the actual number of bits. Maybe I miss something?