-
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
Learn heuristic to pick fastest select_k algorithm #1523
Merged
rapids-bot
merged 4 commits into
rapidsai:branch-23.06
from
benfred:select_k_heuristic2
May 17, 2023
Merged
Changes from 3 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 |
---|---|---|
|
@@ -21,11 +21,101 @@ | |
|
||
#include <raft/core/nvtx.hpp> | ||
|
||
#include <raft/neighbors/detail/selection_faiss.cuh> | ||
#include <rmm/cuda_stream_view.hpp> | ||
#include <rmm/mr/device/device_memory_resource.hpp> | ||
|
||
namespace raft::matrix::detail { | ||
|
||
// this is a subset of algorithms, chosen by running the algorithm_selection | ||
// notebook in cpp/scripts/heuristics/select_k | ||
enum class Algo { kRadix11bits, kWarpDistributedShm, kFaissBlockSelect }; | ||
|
||
/** | ||
* Predict the fastest select_k algorithm based on the number of rows/cols/k | ||
* | ||
* The body of this method is automatically generated, using a DecisionTree | ||
* to predict the fastest algorithm based off of thousands of trial runs | ||
* on different values of rows/cols/k. The decision tree is converted to c++ | ||
* code, which is cut and paste below. | ||
* | ||
* The code to generate is in cpp/scripts/heuristics/select_k, running the | ||
* 'generate_heuristic' notebook there will replace the body of this function | ||
* with the latest learned heuristic | ||
*/ | ||
inline Algo choose_select_k_algorithm(size_t rows, size_t cols, int k) | ||
{ | ||
if (k > 134) { | ||
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. I think, we'd better use log_2(k) instead of k when constructing the heuristic, so that all values of k go in powers of two. For all warp-based algorithms, performance for non-powers of two is equal to their rounded-up powers of two (queue capacity parameter). |
||
if (k > 256) { | ||
if (k > 809) { | ||
return Algo::kRadix11bits; | ||
} else { | ||
if (rows > 124) { | ||
if (cols > 63488) { | ||
return Algo::kFaissBlockSelect; | ||
} else { | ||
return Algo::kRadix11bits; | ||
} | ||
} else { | ||
return Algo::kRadix11bits; | ||
} | ||
} | ||
} else { | ||
if (cols > 678736) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
return Algo::kRadix11bits; | ||
} | ||
} | ||
} else { | ||
if (cols > 13776) { | ||
if (rows > 335) { | ||
if (k > 1) { | ||
if (rows > 546) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
if (k > 17) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
return Algo::kFaissBlockSelect; | ||
} | ||
} | ||
} else { | ||
return Algo::kFaissBlockSelect; | ||
} | ||
} else { | ||
if (k > 44) { | ||
if (cols > 1031051) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
if (rows > 22) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
return Algo::kRadix11bits; | ||
} | ||
} | ||
} else { | ||
return Algo::kWarpDistributedShm; | ||
} | ||
} | ||
} else { | ||
if (k > 1) { | ||
if (rows > 188) { | ||
return Algo::kWarpDistributedShm; | ||
} else { | ||
if (k > 72) { | ||
return Algo::kRadix11bits; | ||
} else { | ||
return Algo::kWarpDistributedShm; | ||
} | ||
} | ||
} else { | ||
return Algo::kFaissBlockSelect; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Select k smallest or largest key/values from each row in the input data. | ||
* | ||
|
@@ -77,15 +167,27 @@ void select_k(const T* in_val, | |
{ | ||
common::nvtx::range<common::nvtx::domain::raft> fun_scope( | ||
"matrix::select_k(batch_size = %zu, len = %zu, k = %d)", batch_size, len, k); | ||
// TODO (achirkin): investigate the trade-off for a wider variety of inputs. | ||
const bool radix_faster = batch_size >= 64 && len >= 102400 && k >= 128; | ||
if (k <= select::warpsort::kMaxCapacity && !radix_faster) { | ||
select::warpsort::select_k<T, IdxT>( | ||
in_val, in_idx, batch_size, len, k, out_val, out_idx, select_min, stream, mr); | ||
} else { | ||
select::radix::select_k<T, IdxT, (sizeof(T) >= 4 ? 11 : 8), 512>( | ||
in_val, in_idx, batch_size, len, k, out_val, out_idx, select_min, true, stream, mr); | ||
|
||
auto algo = choose_select_k_algorithm(batch_size, len, k); | ||
switch (algo) { | ||
case Algo::kRadix11bits: | ||
return detail::select::radix::select_k<T, IdxT, 11, 512>(in_val, | ||
in_idx, | ||
batch_size, | ||
len, | ||
k, | ||
out_val, | ||
out_idx, | ||
select_min, | ||
true, // fused_last_filter | ||
stream); | ||
case Algo::kWarpDistributedShm: | ||
return detail::select::warpsort:: | ||
select_k_impl<T, IdxT, detail::select::warpsort::warp_sort_distributed_ext>( | ||
in_val, in_idx, batch_size, len, k, out_val, out_idx, select_min, stream); | ||
case Algo::kFaissBlockSelect: | ||
return neighbors::detail::select_k( | ||
in_val, in_idx, batch_size, len, out_val, out_idx, select_min, k, stream); | ||
} | ||
} | ||
|
||
} // namespace raft::matrix::detail |
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.
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.
Just a tiny nitpick: