Skip to content

Argument annotation framework for segmented algorithms - #8875

Merged
pciolkosz merged 24 commits into
NVIDIA:mainfrom
pciolkosz:argument_annotations
Jun 2, 2026
Merged

pciolkosz merged 24 commits into
NVIDIA:mainfrom
pciolkosz:argument_annotations

Conversation

@pciolkosz

@pciolkosz pciolkosz commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Introduces a flexible argument annotation framework that allows users to annotate algorithm parameters with compile-time and runtime bounds, enabling dispatch-layer optimizations without combinatorial explosion of function overloads.

This is a prototype implementation of Option 1 from #7495: annotating parameters with wrappers on the parameter itself.

Resolves #7495

Design decisions

Three argument wrappers + plain values

  • Plain values work as-is — no wrapper required. A scalar is a single value, a pointer/span is a collection.
  • argument::deferred{indirect_value, bounds...} and argument::deferred_sequence{sequence, bounds...} — device-resident value not accessible at API call time. User explicitly specifies if the argument holds a single value or a sequence with the wrapper type. Host-side dispatch can only use bounds, not actual values.
  • This explicit separation of value vs sequence was applied to other wrappers as well for consistency, even if in those cases the type system could be used to infer it.
  • **argument::constant<Value> and argument::constant_sequence<Seq>** — compile-time constant. Supports scalars and arrays in the sequence case (e.g., argument::constant_sequence<std::array{128, 256, 512}>`). Bounds are derived from the value itself.
  • argument::immediate{value, bounds...} and argument::immediate_sequence{seq, bounds...} — runtime value with optional static and/or runtime bounds. Stores any type (scalar, span, pointer, etc.), but for multiple values immediate_sequence should be used.

Bounds are separate types created via factory functions

  • cuda::argument::bounds<1, 8>() — static bounds, type deduced from NTTPs (no need to spell the value type)
  • cuda::argument::bounds(rt_min, rt_max) — runtime bounds, type deduced via CTAD
  • Both can be provided: argument::dynamic{k, argument::bounds<1, 256>(), argument::bounds(50, 200)}
  • Static bounds are part of the wrapper's type for compile-time specialization
  • Runtime bounds are stored as a member with full-range defaults

Unified access via argument::traits and free functions

  • argument::traits<T> — type-level access: is_deferred, value_type, element_type, static_min, static_max. Works in if constexpr and static_assert.
  • argument::unwrap(arg) — extracts the raw value from any wrapper or passes plain values through
  • argument::static_min(arg) / argument::static_max(arg) — free functions for static bounds
  • argument::min(arg) / argument::max(arg) — free functions for runtime bounds
  • __is_single_value_v<T> — internal trait: true for scalars, enums, and span<T, 1>
  • All accessors work uniformly on plain values, argument::constant, argument::dynamic, and argument::deferred_*

What's not covered (future work)

  • Discrete enum option sets (e.g., ascending/descending)
  • Device-accessible bounds (computed by prior kernels)
  • Environment-based guarantees for non-parameter properties (e.g., total_num_items)

@copy-pr-bot

copy-pr-bot Bot commented May 8, 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 May 8, 2026
@pciolkosz

Copy link
Copy Markdown
Contributor Author

/ok to test b4a8009

@github-actions

This comment has been minimized.

Comment on lines +72 to +79
//! @brief Wraps a compile-time constant argument value.
template <auto _Value>
struct constant
{
using value_type = decltype(_Value);
using __element_type = __element_type_of_t<value_type>;
static constexpr value_type value = _Value;
};

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.

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.

constant_wrapper is quite problematic. I've already tried to introduce it to libcu++, but there are problems with value member being const auto& to the template parameter storage, which causes nvcc to be unhappy about using __host__ stuff in __global__/__device__ code. I tried some workarounds, but unsuccessfully :/

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.

I think the type is simple enough, that once we add cuda::std::constant_wrapper we can alias them or at least make them compatible. constant_wrapper has a bunch of extra stuff we don't need here and I would not block this work waiting on the std type

@pciolkosz
pciolkosz marked this pull request as ready for review May 11, 2026 17:05
@pciolkosz
pciolkosz requested a review from a team as a code owner May 11, 2026 17:05
@pciolkosz
pciolkosz requested a review from ericniebler May 11, 2026 17:05
@cccl-authenticator-app cccl-authenticator-app Bot moved this from In Progress to In Review in CCCL May 11, 2026
@github-actions

This comment has been minimized.

@pciolkosz
pciolkosz force-pushed the argument_annotations branch from 4aa9fcc to ded784d Compare May 11, 2026 22:24
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@miscco miscco 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.

The more I read about this the less I am convinced the this is the right approach.

I find the interface unintuitive.

  • Why are there static and runtime bounds?
  • What is bounded in case of sequences? Is it the size of the sequence or the values?
  • the deferred value is not working with non-segments, and its dangerous to use because it has host device accessors

There is a huge machinery that I am not sure is necessary. Comparing this with what we already have in mdspan

template <class _Tp>
[[nodiscard]] _CCCL_API constexpr auto __de_ice(_Tp) noexcept
{
  if constexpr (__integral_constant_like<_Tp>)
  {
    return _Tp::value;
  }
  else
  {
    return __val;
  }
}

We can extend this to

template <class _Tp>
[[nodiscard]] _CCCL_API constexpr auto __max(_Tp) noexcept
{
  if constexpr (__integral_constant_like<_Tp>)
  {
    return _Tp::value;
  }
  else
  {
    return numeric_limits<_Tp>::max();
  }
}

I do not see what we need to much machinery for. This also extents to span<_Arg, 4> where you either pass an int or an integral_constant

Comment on lines +60 to +65
// Pointers
template <class _Tp>
struct __element_type_of<_Tp*>
{
using type = _Tp;
};

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.

Should this only handle pointers?

There is pointer_traits, which susses out the element type of any contiguous iterator

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.

I reworked it to use to use iter_value_t instead, pointer_traits was too broad, accepting any templated type.

//!
//! Supports both scalar values and arrays (e.g., @c __constant<std::array{128, 256, 512}>).
//! For arrays, bounds are computed as the min/max of the elements.
template <auto _Value>

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.

Conventionally this is usually template<typename _Tp, _Tp _Value>

@pciolkosz pciolkosz May 19, 2026 •

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.

I would prefer if users wouldn't need to specify the type and use NTTP instead

__validate_static_bounds<_ElementType, _StaticBounds>();
if constexpr (::cuda::std::is_same_v<_StaticBounds, __no_bounds>)
{
return ::cuda::std::numeric_limits<_ElementType>::lowest();

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.

We need to check whether numeric_limits has been specialized, otherwise this might blow up

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.

I would separate this to another PR, because I am not sure what to do if they are not specialized. I don't think we can just blanket static assert

Comment on lines +192 to +197
template <class _ElementType, class _StaticBounds>
_CCCL_API constexpr _ElementType __effective_lowest(__runtime_bounds<_ElementType> __runtime_bounds) noexcept
{
auto __static_lowest = __wrapper_static_lowest<_ElementType, _StaticBounds>();
return __static_lowest > __runtime_bounds.lowest ? __static_lowest : __runtime_bounds.lowest;
}

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.

This is a programming error that should never occur. We are comparing against ::cuda::std::numeric_limits<_ElementType>::lowest(); which by definition is the lowest possible value.

This condition cannot ever be true except maybe for floating point types, but then its the wrong check

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.

Lowest is not always numeric limits, sometimes it's the lower end of the specified bounds, which is where this check is needed. In case its the numeric limits the check is still correct

Comment on lines +61 to +67
if constexpr (cuda::argument::__traits<_SegSizeArg>::is_single_value)
{
return static_cast<int>(__val);
}
else
{
int __total = 0;

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: Why do we need the special casing and not just have the single value argument return a static constexpr size of 1?

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.

It's just some dummy work that actually uses the argument, what exactly it does is not that relevant. Returning the value and summing up sequences seemed like the simplest thing that touches the argument provided

{
constexpr auto seg_sizes = cuda::argument::__constant<cuda::std::array{64, 128, 256}>{};
static_assert(select_variant(seg_sizes) == algorithm_variant::shared_memory);
assert(compute_buffer_size(seg_sizes, 3) == 256 * 3);

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.

Why is this returning 3 * 256 and not the proper size?

@pciolkosz pciolkosz May 19, 2026 •

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.

Its just how compute_buffer_size was implemented, it could be smarter for constant argument, but for the test it was supposed to test the dynamic max getter


// dynamic_argument: tight static bounds, shared memory, buffer = static max
{
constexpr auto seg_size = cuda::argument::__immediate{100, cuda::argument::__bounds<1, 256>()};

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.

I am not a fan of this type. I believe the whole design would be much more flexible if this would be a wrapper around a value / static

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.

Would you like to elaborate on that?

Comment on lines +156 to +157
auto seg_sizes = cuda::argument::__immediate{
cuda::std::span<int>{sizes, 3}, cuda::argument::__bounds<1, 256>(), cuda::argument::__bounds(1, 200)};

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.

I find this unclear, what are the bounds? Is this the size of the span?, is it the values within the span?

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.

Its for the values within the span, the bounds are supposed to communicate the limits for values of the argument, regardless if its a single value or a sequence

@pauleonix pauleonix May 20, 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.

I somewhat do see @miscco's issue here. CUB APIs where we want to use this currently take pointers/iterators and have separate arguments for the size(s) of the input/output range(s). The thought is that this framework can be used to specify bounds for both kinds of arguments separately (e.g. give limits for the number of segments and for the segment sizes). For more modern APIs that would take a span or other range (combining sth iterator-like with a size) this is icky.

Not sure what the solution is. For the current work in CUB there is no need to work with spans, but it does seem somewhat backward to not support them.

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.

Right now you can repeat the number of elements both in the span and as a separate argument. Then you have a chance to add element bounds on the span and the bounds for the number of elements on the separately passed size.

We could introduce two types of bounds and annotate both of those things on the span, but that would make the wrapper span argument complicated. But I am not against that idea if we think its needed

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.

I'm rather saying that anything taking ranges/spans is not needed for the MVP, so with spans being confusing we could opt to pass just iterators for both the multi-value and the deferred-value case for now.

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.

Assuming that the current implementation works with (fancy) iterators, we can keep it this way for now.

Comment on lines +166 to +171
int val = 100;
auto seg_size = cuda::argument::__deferred_value{
cuda::std::span<int, 1>{&val, 1}, cuda::argument::__bounds<1, 256>(), cuda::argument::__bounds(1, 200)};
static_assert(cuda::argument::__traits<decltype(seg_size)>::max <= shared_memory_capacity);
assert(select_variant(seg_size) == algorithm_variant::shared_memory);
assert(compute_buffer_size(seg_size, 4) == 200 * 4);

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.

This does not work properly, how would a __deferred_value that is a single element look like? requiring it to be value initialized forces the user to use a span, without telling them to.

I believe that would need to be clear

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.

That is the design of the deferred argument, it will most likely hold a span or a pointer

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.

Or a fancy iterator.

@github-actions

This comment has been minimized.

Comment on lines +249 to +255
if constexpr (!::cuda::std::is_same_v<_StaticBounds, __no_bounds>)
{
_CCCL_ASSERT(__val >= __static_bounds_.lowest(), "immediate argument value is below static lowest bound");
_CCCL_ASSERT(__val <= __static_bounds_.max(), "immediate argument value is above static max bound");
}
_CCCL_ASSERT(__val >= __runtime_bounds_.lowest, "immediate argument value is below runtime lowest bound");
_CCCL_ASSERT(__val <= __runtime_bounds_.max, "immediate argument value is above runtime max bound");

@pauleonix pauleonix May 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.

Suggestion: _CCCL_THROW looks like a good option (asserts on device afaiu).

{
using __element_type = __element_type_of_t<_Arg>;

_Arg arg;

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.

I think the confusion of @miscco is specifically with this being the deferred case. But just pointers would not be enough since we need to support fancy iterators.

//!
//! @tparam _Lowest The static lower bound.
//! @tparam _Max The static upper bound.
template <auto _Lowest, auto _Max>

@pauleonix pauleonix May 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.

It should be the same type for both limits (and ideally the actual value). Anything else looks like a mistake on the user side that should be reported earlier rather than later.

Comment on lines +156 to +157
auto seg_sizes = cuda::argument::__immediate{
cuda::std::span<int>{sizes, 3}, cuda::argument::__bounds<1, 256>(), cuda::argument::__bounds(1, 200)};

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.

Assuming that the current implementation works with (fancy) iterators, we can keep it this way for now.

// __is_single_value_v
// =====================================================================

// TODO(pciolkosz): Improve this for more single value cases like optional etc.

@pauleonix pauleonix May 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.

Critical: For deferred arguments the is_single_value trait is given by the wrapper type (deferred_value -> single value, deferred_sequence -> not single value). For other argument types (immediate and constant) it depends on their type according to this variable template. I don't like the inconsistency of that.

AFAIK CUB does not really need an immediate or constant sequence, but if we want to support them, I think adding explicit immediate_sequence etc. and maybe shortening deferred_value to deferred for parity would be a good way forward.

The easier way might be just restricting immediate and constant to scalar arithmetic types (and rename to *_value) for now until there is a use-case for passing immediate/constant ranges.

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.

I switched to constant/immediate and constant_sequence/immediate_sequence

__validate_bounds();
}

template <auto _Lowest, auto _Max>

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.

Suggested change
template <auto _Lowest, auto _Max>
template <_Arg _Lowest, _Arg _Max>

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.

It would be inconvenient to require the same type to be specified on the bounds and the argument. Let's say you have int64 array, but you want to say its in bounds<0, 1000>, you would need to remember to make sure you don't use ints for the bounds. I instead accept any type in the bounds and cast it to the type of the argument when I create the argument wrapper

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.

you would need to remember to make sure you don't use ints for the bounds

Doesn't the compiler just convert the int to int64? That should be no problem. Like std::integral_constant<int64_t, 42> is fine too.

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.

The problem is those two things are separate, when you do:
cuda::argument::immediate arg{42ULL, cuda::argument::bounds<0, 420>()} the bounds expression knows nothing about the int64 and it will construct bounds object holding an int, which I think we should accept in the argument wrapper and convert the bounds to int64

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.

I would have been ok with forcing the user to use the correct/fitting literals (assuming they are literals at all). I think it is good style anyway. Maybe we should discuss this in the next review meeting.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

{
using __element_type = __element_type_of_t<_Arg>;

_Arg arg;

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.

Then we should make it ugly

Suggested change
_Arg arg;
_Arg __arg_;

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

🥳 CI Workflow Results

🟩 Finished in 1h 18m: Pass: 100%/110 | Total: 1d 07h | Max: 52m 31s | Hits: 99%/317546

See results here.

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.

Design proposal: A flexible parameter and guarantees framework for (segmented) algorithms

7 participants