Repository navigation
Argument annotation framework for segmented algorithms - #8875
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. |
|
/ok to test b4a8009 |
This comment has been minimized.
This comment has been minimized.
| //! @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; | ||
| }; |
There was a problem hiding this comment.
Should this maybe be based on std::constant_wrapper? https://wg21.link/p2781
There was a problem hiding this comment.
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 :/
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
4aa9fcc to
ded784d
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
miscco
left a comment
There was a problem hiding this comment.
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
| // Pointers | ||
| template <class _Tp> | ||
| struct __element_type_of<_Tp*> | ||
| { | ||
| using type = _Tp; | ||
| }; |
There was a problem hiding this comment.
Should this only handle pointers?
There is pointer_traits, which susses out the element type of any contiguous iterator
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
Conventionally this is usually template<typename _Tp, _Tp _Value>
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
We need to check whether numeric_limits has been specialized, otherwise this might blow up
There was a problem hiding this comment.
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
| 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; | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
| if constexpr (cuda::argument::__traits<_SegSizeArg>::is_single_value) | ||
| { | ||
| return static_cast<int>(__val); | ||
| } | ||
| else | ||
| { | ||
| int __total = 0; |
There was a problem hiding this comment.
Question: Why do we need the special casing and not just have the single value argument return a static constexpr size of 1?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Why is this returning 3 * 256 and not the proper size?
There was a problem hiding this comment.
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>()}; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Would you like to elaborate on that?
| auto seg_sizes = cuda::argument::__immediate{ | ||
| cuda::std::span<int>{sizes, 3}, cuda::argument::__bounds<1, 256>(), cuda::argument::__bounds(1, 200)}; |
There was a problem hiding this comment.
I find this unclear, what are the bounds? Is this the size of the span?, is it the values within the span?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Assuming that the current implementation works with (fancy) iterators, we can keep it this way for now.
| 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); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
That is the design of the deferred argument, it will most likely hold a span or a pointer
This comment has been minimized.
This comment has been minimized.
| 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"); |
There was a problem hiding this comment.
Suggestion: _CCCL_THROW looks like a good option (asserts on device afaiu).
| { | ||
| using __element_type = __element_type_of_t<_Arg>; | ||
|
|
||
| _Arg arg; |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
| auto seg_sizes = cuda::argument::__immediate{ | ||
| cuda::std::span<int>{sizes, 3}, cuda::argument::__bounds<1, 256>(), cuda::argument::__bounds(1, 200)}; |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I switched to constant/immediate and constant_sequence/immediate_sequence
| __validate_bounds(); | ||
| } | ||
|
|
||
| template <auto _Lowest, auto _Max> |
There was a problem hiding this comment.
| template <auto _Lowest, auto _Max> | |
| template <_Arg _Lowest, _Arg _Max> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
| { | ||
| using __element_type = __element_type_of_t<_Arg>; | ||
|
|
||
| _Arg arg; |
There was a problem hiding this comment.
Then we should make it ugly
| _Arg arg; | |
| _Arg __arg_; |
🥳 CI Workflow Results🟩 Finished in 1h 18m: Pass: 100%/110 | Total: 1d 07h | Max: 52m 31s | Hits: 99%/317546See results here. |
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
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.argument::constant<Value>andargument::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...}andargument::immediate_sequence{seq, bounds...}— runtime value with optional static and/or runtime bounds. Stores any type (scalar, span, pointer, etc.), but for multiple valuesimmediate_sequenceshould 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 CTADargument::dynamic{k, argument::bounds<1, 256>(), argument::bounds(50, 200)}Unified access via
argument::traitsand free functionsargument::traits<T>— type-level access:is_deferred,value_type,element_type,static_min,static_max. Works inif constexprandstatic_assert.argument::unwrap(arg)— extracts the raw value from any wrapper or passes plain values throughargument::static_min(arg)/argument::static_max(arg)— free functions for static boundsargument::min(arg)/argument::max(arg)— free functions for runtime bounds__is_single_value_v<T>— internal trait: true for scalars, enums, andspan<T, 1>argument::constant,argument::dynamic, andargument::deferred_*What's not covered (future work)
total_num_items)