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

Shared-memory-cached kernel for reduce_cols_by_key to limit atomic conflicts #1050

Merged
merged 4 commits into from
Dec 8, 2022

Conversation

Nyrio
Copy link
Contributor

@Nyrio Nyrio commented Nov 28, 2022

reduce_cols_by_key is used in k-means to compute the total weight in each cluster. When the number of clusters is really small, atomic conflicts make the reduction effectively sequential. This can be avoided by using a shared-memory cache.

This PR adds a cached kernel and some heuristics to decide when to use it, resulting in large speedups for reduce_cols_by_keys and in turn ~15% speedups in some cases for k-means.

@Nyrio Nyrio added 3 - Ready for Review improvement Improvement / enhancement to an existing function non-breaking Non-breaking change cpp CMake labels Nov 28, 2022
@Nyrio Nyrio requested review from a team as code owners November 28, 2022 17:07
@Nyrio
Copy link
Contributor Author

Nyrio commented Nov 28, 2022

Comparison of the performance of the direct vs cached kernels.

2022-11-28_reduce_cols_by_key_perf

@Nyrio Nyrio changed the base branch from branch-22.12 to branch-23.02 November 30, 2022 13:47
*/
template <typename ElementType, typename KeyType = ElementType, typename IndexType = std::uint32_t>
void reduce_cols_by_key(
const raft::handle_t& handle,
raft::device_matrix_view<const ElementType, IndexType, raft::row_major> data,
raft::device_vector_view<const KeyType, IndexType> keys,
raft::device_matrix_view<ElementType, IndexType, raft::row_major> out,
IndexType nkeys)
IndexType nkeys,
Copy link
Member

Choose a reason for hiding this comment

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

Not part of this PR, but I wonder if there could someday be an overload of this function that would compute this value when not specified.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, but what kind of use cases do you have in mind? Since this primitive works with keys in the range [0, nkeys[ it's fair to assume nkeys will typically be known. For a more generic primitive working with arbitrary keys, we would first need to map them to such a range, and we can compute nkeys in the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless you mean that instead of passing nkeys we should get it from the dimensions of out, which actually makes a lot of sense.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I guess the latter option makes sense. I was considering doing something similar in the knn APIs, since the shape of the output matrices already tell us k.

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 see the following options, let me know if you have a preference:

  • Give nkeys a default value and ignore it, using output dims instead (might break compilation due to unused variable warning?)
  • Give nkeys a default value and if not overridden by the user, get it from the output dims.
  • Remove nkeys arg (potentially breaking but I doubt anyone is using this prim outside the raft codebase?)

Copy link
Contributor

Choose a reason for hiding this comment

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

The second option looks good to me.

Copy link
Member

Choose a reason for hiding this comment

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

@Nyrio do you want me to go ahead and merge this PR in the meantime?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we aren't in a hurry, I just did the change in this PR. I'd rather put nkeys in last position so we can specify reset_sums and ignore nkeys, but in the interest of not breaking the API I can't do that. If we don't mind breaking the API, I can simply remove the arg and always infer.

Copy link
Contributor

@tfeher tfeher left a comment

Choose a reason for hiding this comment

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

Hi Louis, it is great to see this improvement! I have just a few small comments.

cpp/include/raft/linalg/detail/reduce_cols_by_key.cuh Outdated Show resolved Hide resolved
cpp/include/raft/linalg/detail/reduce_cols_by_key.cuh Outdated Show resolved Hide resolved
Copy link
Contributor

@tfeher tfeher left a comment

Choose a reason for hiding this comment

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

Thanks Louis for addressing the issues, the PR looks good to me!

*/
template <typename ElementType, typename KeyType = ElementType, typename IndexType = std::uint32_t>
void reduce_cols_by_key(
const raft::handle_t& handle,
raft::device_matrix_view<const ElementType, IndexType, raft::row_major> data,
raft::device_vector_view<const KeyType, IndexType> keys,
raft::device_matrix_view<ElementType, IndexType, raft::row_major> out,
IndexType nkeys)
IndexType nkeys,
Copy link
Contributor

Choose a reason for hiding this comment

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

The second option looks good to me.

@cjnolet
Copy link
Member

cjnolet commented Dec 8, 2022

@gpucibot merge

@rapids-bot rapids-bot bot merged commit 9ec9b70 into rapidsai:branch-23.02 Dec 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5 - Ready to Merge CMake cpp improvement Improvement / enhancement to an existing function non-breaking Non-breaking change
Projects
Development

Successfully merging this pull request may close these issues.

3 participants