-
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
Shared-memory-cached kernel for reduce_cols_by_key
to limit atomic conflicts
#1050
Conversation
*/ | ||
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, |
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.
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.
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.
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.
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.
Unless you mean that instead of passing nkeys
we should get it from the dimensions of out
, which actually makes a lot of sense.
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.
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.
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 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?)
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 second option looks good to me.
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.
@Nyrio do you want me to go ahead and merge this PR in the meantime?
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 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.
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.
Hi Louis, it is great to see this improvement! I have just a few small comments.
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.
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, |
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 second option looks good to me.
@gpucibot merge |
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.