Repository navigation
Implement the new tuning API for detail::batched_topk::dispatch_batched_topk - #8538
Conversation
|
Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually. Contributors can view more details about this message here. |
ff3b09b to
53da22b
Compare
6d61a0f to
158e4c6
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e3952c3 to
6f33a7a
Compare
This comment has been minimized.
This comment has been minimized.
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Index, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl; | ||
|
|
||
| // Base case: End of policy chain reached: If we reach Index == Count, it means we checked all with no match | ||
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl<PoliciesT, Count, Count, WorkerPerSegmentAgentT, AgentParamsT...> | ||
| { | ||
| using policy_t = void; | ||
| static constexpr bool has_valid_policy = false; | ||
| }; | ||
|
|
||
| // Policies are ordered by decreasing tile size. This finds the first (i.e., largest) policy whose agent | ||
| // TempStorage fits within the static shared memory limit (max_smem_per_block, typically 48KB). | ||
| // This is useful to figure out which segments (given a runtime segment size) can still be addressed with a | ||
| // one-worker-per-segment approach. | ||
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Index, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl | ||
| { | ||
| // Inspect the current policy | ||
| using current_policy_t = ::cuda::std::tuple_element_t<Index, PoliciesT>; | ||
|
|
||
| // Instantiate agent to check temporary storage size | ||
| using current_agent_t = WorkerPerSegmentAgentT<current_policy_t, AgentParamsT...>; | ||
| static constexpr bool fits = (sizeof(typename current_agent_t::TempStorage) <= max_smem_per_block); | ||
|
|
||
| // The 'next' policy in the chain | ||
| using next_step = | ||
| find_first_smem_fitting_policy_impl<PoliciesT, Index + 1, Count, WorkerPerSegmentAgentT, AgentParamsT...>; | ||
|
|
||
| // Select result: | ||
| // If 'fits' is true, we stop here. | ||
| // If 'fits' is false, we take the result from 'next_step'. | ||
| using policy_t = ::cuda::std::conditional_t<fits, current_policy_t, typename next_step::policy_t>; | ||
|
|
||
| // Whether there's a valid policy that we can instantiate the agent with such that the agent's shared memory doesn't | ||
| // exceed the static shared memory limimt | ||
| static constexpr bool has_valid_policy = fits ? true : next_step::has_valid_policy; | ||
| }; |
There was a problem hiding this comment.
Remark: This code was entirely unused.
There was a problem hiding this comment.
Yeah, the motivation for find_first_smem_fitting_policy is that it will be needed once we support larger-than-what-fits-into-one-worker-per-segment segments, as we want to figure out how large of a segment size we can support "in-place" by a single worker. During the PR review meeting where we reviewed the PR that made find_smallest_covering_policy superseed find_first_smem_fitting_policy, we agreed to keep this part around until that's the case. But I'm also ok to copy it back in, once needed.
| constexpr bool covers = tile_size >= max_segment_size; | ||
| constexpr bool fits_smem = sizeof(typename candidate_agent_t::TempStorage) <= max_smem_per_block; | ||
| constexpr int next = find_index<Index + 1>(); | ||
| if constexpr (covers && fits_smem) | ||
| { | ||
| return next >= 0 ? next : Index; |
There was a problem hiding this comment.
Important remark: I reviewed the initial implementation claude generated and noticed it did not include the SMEM check (fits_smem), so I instructed it to add it. I only later noticed that the entire code around find_first_smem_fitting_policy_impl was actually unused. So the logic implemented here differs from the original implementation, since it finds the smallest covering policy that ALSO fits into SMEM. I think this is an improvement, but I am fine removing this extra again.
There was a problem hiding this comment.
Thanks for pointing this out. In my mind, I assumed that as tile sizes are decreasing, I assumed strictly non-increasing shared memory requirements. That will probably hold for some time to come, but I think the extra check won't hurt.
| cudaStream_t stream = nullptr, | ||
| [[maybe_unused]] PolicySelector policy_selector = {}) | ||
| { | ||
| using offset_t = ::cuda::std::int64_t; |
There was a problem hiding this comment.
Remark: this was also unused
6f33a7a to
c713e2e
Compare
This comment has been minimized.
This comment has been minimized.
elstehle
left a comment
There was a problem hiding this comment.
Thanks a lot for taking this on! Changes look good to me.
| { | ||
| // The list of per-segment agent policies is ordered by decreasing tile size. At compile time, the smallest policy | ||
| // whose tile size still covers the upper bound of the segment size is selected. | ||
| ::cuda::std::array<worker_policy, 6> worker_per_segment_policies; |
There was a problem hiding this comment.
question: How would we envision this to work with varying number of worker_per_segment_policies. E.g., if sm_120 had 6, but sm_80 had only 4 worker_per_segment_policies?
There was a problem hiding this comment.
We can't in the current design. We would need a container of dynamic size that works in constexpr land. If you can give me some maximum, we could use cuda::std::inplace_vector. Otherwise we are going to have a hard time :S
| constexpr bool covers = tile_size >= max_segment_size; | ||
| constexpr bool fits_smem = sizeof(typename candidate_agent_t::TempStorage) <= max_smem_per_block; | ||
| constexpr int next = find_index<Index + 1>(); | ||
| if constexpr (covers && fits_smem) | ||
| { | ||
| return next >= 0 ? next : Index; |
There was a problem hiding this comment.
Thanks for pointing this out. In my mind, I assumed that as tile sizes are decreasing, I assumed strictly non-increasing shared memory requirements. That will probably hold for some time to come, but I think the extra check won't hurt.
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Index, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl; | ||
|
|
||
| // Base case: End of policy chain reached: If we reach Index == Count, it means we checked all with no match | ||
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl<PoliciesT, Count, Count, WorkerPerSegmentAgentT, AgentParamsT...> | ||
| { | ||
| using policy_t = void; | ||
| static constexpr bool has_valid_policy = false; | ||
| }; | ||
|
|
||
| // Policies are ordered by decreasing tile size. This finds the first (i.e., largest) policy whose agent | ||
| // TempStorage fits within the static shared memory limit (max_smem_per_block, typically 48KB). | ||
| // This is useful to figure out which segments (given a runtime segment size) can still be addressed with a | ||
| // one-worker-per-segment approach. | ||
| template <typename PoliciesT, | ||
| ::cuda::std::int64_t Index, | ||
| ::cuda::std::int64_t Count, | ||
| template <typename...> class WorkerPerSegmentAgentT, | ||
| typename... AgentParamsT> | ||
| struct find_first_smem_fitting_policy_impl | ||
| { | ||
| // Inspect the current policy | ||
| using current_policy_t = ::cuda::std::tuple_element_t<Index, PoliciesT>; | ||
|
|
||
| // Instantiate agent to check temporary storage size | ||
| using current_agent_t = WorkerPerSegmentAgentT<current_policy_t, AgentParamsT...>; | ||
| static constexpr bool fits = (sizeof(typename current_agent_t::TempStorage) <= max_smem_per_block); | ||
|
|
||
| // The 'next' policy in the chain | ||
| using next_step = | ||
| find_first_smem_fitting_policy_impl<PoliciesT, Index + 1, Count, WorkerPerSegmentAgentT, AgentParamsT...>; | ||
|
|
||
| // Select result: | ||
| // If 'fits' is true, we stop here. | ||
| // If 'fits' is false, we take the result from 'next_step'. | ||
| using policy_t = ::cuda::std::conditional_t<fits, current_policy_t, typename next_step::policy_t>; | ||
|
|
||
| // Whether there's a valid policy that we can instantiate the agent with such that the agent's shared memory doesn't | ||
| // exceed the static shared memory limimt | ||
| static constexpr bool has_valid_policy = fits ? true : next_step::has_valid_policy; | ||
| }; |
There was a problem hiding this comment.
Yeah, the motivation for find_first_smem_fitting_policy is that it will be needed once we support larger-than-what-fits-into-one-worker-per-segment segments, as we want to figure out how large of a segment size we can support "in-place" by a single worker. During the PR review meeting where we reviewed the PR that made find_smallest_covering_policy superseed find_first_smem_fitting_policy, we agreed to keep this part around until that's the case. But I'm also ok to copy it back in, once needed.
🥳 CI Workflow Results🟩 Finished in 13h 07m: Pass: 100%/269 | Total: 10d 07h | Max: 2h 28m | Hits: 40%/178731See results here. |
cub.bench.segmented_topk.keys.basefor SM120Fixes: #8479