Skip to content

Implement the new tuning API for detail::batched_topk::dispatch_batched_topk - #8538

Merged
bernhardmgruber merged 11 commits into
NVIDIA:mainfrom
bernhardmgruber:tune_batched_topk
Apr 22, 2026
Merged

bernhardmgruber merged 11 commits into
NVIDIA:mainfrom
bernhardmgruber:tune_batched_topk

Conversation

@bernhardmgruber

@bernhardmgruber bernhardmgruber commented Apr 20, 2026 •

Copy link
Copy Markdown
Contributor
  • No SASS changes for cub.bench.segmented_topk.keys.base for SM120

Fixes: #8479

@copy-pr-bot

copy-pr-bot Bot commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

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.

@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Progress in CCCL Apr 20, 2026
@bernhardmgruber
bernhardmgruber marked this pull request as ready for review April 21, 2026 08:28
@bernhardmgruber
bernhardmgruber requested review from a team as code owners April 21, 2026 08:28
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL Apr 21, 2026
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Comment on lines -35 to -83
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;
};

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remark: This code was entirely unused.

@elstehle elstehle Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines +60 to +65
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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remark: this was also unused

@github-actions

This comment has been minimized.

@elstehle elstehle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

Comment on lines +60 to +65
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment on lines -35 to -83
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;
};

@elstehle elstehle Apr 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@bernhardmgruber
bernhardmgruber enabled auto-merge (squash) April 22, 2026 08:47
@github-actions

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 13h 07m: Pass: 100%/269 | Total: 10d 07h | Max: 2h 28m | Hits: 40%/178731

See results here.

@bernhardmgruber
bernhardmgruber merged commit 2247943 into NVIDIA:main Apr 22, 2026
533 of 577 checks passed
@github-project-automation github-project-automation Bot moved this from In Review to Done in CCCL Apr 22, 2026
@bernhardmgruber
bernhardmgruber deleted the tune_batched_topk branch April 22, 2026 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Implement the new tuning API for detail::batched_topk::dispatch_batched_topk

2 participants