Repository navigation
feat(cuda-device): add f32 and f64 warp reduce utilities - #475
Merged
Merged
Conversation
`warp::redux_sync_*` is integer-only - the verified surface is
`redux_sync_add/and/or/xor` plus `redux_sync_{min,max}_{i32,u32}`, with no
float form. That mirrors PTX, where `redux.sync` defines only integer operand
types, so it is a hardware limit rather than a gap in the bindings.
The consequence is that every user reimplementing a float reduction writes the
same butterfly shuffle by hand. This adds it once, for both precisions:
reduce_sum_f32 reduce_max_f32 reduce_min_f32
reduce_sum_f64 reduce_max_f64 reduce_min_f64
Each is five XOR shuffles at offsets 16, 8, 4, 2, 1, built on the existing
`shuffle_xor_f32`/`shuffle_xor_f64`. Every lane ends with the reduced value,
not just lane 0, which is what lets a caller feed the result straight into a
block-level reduction without a second barrier.
Safe functions: these are warp-synchronous but take no pointers and require no
convergence the shuffle intrinsics do not already document, so there is no
additional obligation to put behind `unsafe`.
`#[must_use]` on all six - the return value is the entire point, so discarding
one is always a mistake.
Signed-off-by: abder <[email protected]>
All six reduce_{sum,max,min}_{f32,f64} utilities now carry the same
participation contract as redux_sync_add: the shuffles inside use the
full-warp mask (u32::MAX), so all 32 lanes must be converged and
participate, and calling from divergent control flow or from a block
with fewer than 32 threads is undefined.
Also spell out NaN behavior: sum propagates any NaN to every lane,
while max/min ignore a NaN in one lane (f32::max/f32::min return the
non-NaN operand), so the result is NaN only if every lane holds NaN.
The module-level doc example now calls reduce_sum_f32 instead of
hand-rolling the butterfly it packages, and a host-side signature
stability test pins the six new fn signatures alongside the existing
legacy 64-bit shuffle one.
Signed-off-by: nihalpasham <[email protected]>
Add four kernels covering the new warp reduce helpers:
warp_reduce_sum_util, warp_reduce_max_util, warp_reduce_min_util
(reduce_{sum,max,min}_f32) and warp_reduce_sum_f64_util
(reduce_sum_f64). Each lane writes the reduced value it received, so
the host verifies the every-lane broadcast property, not just lane 0.
Inputs are lane indices 0..32 per warp, making the expected values
exact in f32: sum = 496, max = 31, min = 0 (and 496 again for f64),
so the host compares exactly. The min output buffer is seeded with a
-1.0 sentinel because the expected minimum equals the zeroed-buffer
value.
README updated with the new kernels, a packaged-utilities section
restating the full-warp participation contract, and the expected
output for the two new test sections.
Signed-off-by: nihalpasham <[email protected]>
Collaborator
|
Thanks, I pushed two commits:
Verified on an RTX 5090. Related to #390: Merging. |
nihalpasham
approved these changes
Jul 27, 2026
5 of 26 tasks
This was referenced Jul 27, 2026
8 of 9 tasks
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
warp::redux_sync_*is integer-only. The surface isredux_sync_add/and/or/xorplusredux_sync_{min,max}_{i32,u32}, with no float form — which mirrors PTX, whereredux.syncdefines only integer operand types. It is a hardware limit, not a gap in the bindings.The consequence is that anyone reducing floats across a warp writes the same butterfly shuffle by hand. This adds it once, for both precisions:
Each is five XOR shuffles at offsets 16, 8, 4, 2, 1, built on the existing
shuffle_xor_f32/shuffle_xor_f64.Notes for review
Every lane ends with the reduced value, not just lane 0. That is deliberate — it lets a caller feed the result straight into a block-level reduction without a second barrier, and it is the property a block reduce built on top of these depends on.
Safe functions, not
unsafe. These are warp-synchronous but take no pointers and require no convergence beyond what the underlying shuffle intrinsics already document, so there is no additional obligation to put behindunsafe.#[must_use]on all six: the return value is the entire point, so discarding one is always a mistake.Uses
f32::max/f32::minfor the max and min variants rather than an open-coded compare, so the NaN behaviour follows whatever those lower to rather than being independently specified here.fmt --checkclean, no warnings, no new dependencies.