Skip to content

feat(cuda-device): add f32 and f64 warp reduce utilities - #475

Merged
nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:up/warp-reduce-f32
Jul 27, 2026
Merged

nihalpasham merged 3 commits into
NVIDIA:mainfrom
honeyspoon:up/warp-reduce-f32

Conversation

@honeyspoon

Copy link
Copy Markdown
Contributor

warp::redux_sync_* is integer-only. The surface is redux_sync_add/and/or/xor plus redux_sync_{min,max}_{i32,u32}, with no float form — which mirrors PTX, where redux.sync defines 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:

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.

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 behind unsafe.

#[must_use] on all six: the return value is the entire point, so discarding one is always a mistake.

Uses f32::max / f32::min for 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 --check clean, no warnings, no new dependencies.

`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]>
@nihalpasham nihalpasham added enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings labels Jul 27, 2026
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]>
@nihalpasham

Copy link
Copy Markdown
Collaborator

Thanks,

I pushed two commits:

  • the six doc comments now state the full-warp convergence contract and NaN behavior (mirroring redux_sync_add), and
  • the warp_reduce example exercises reduce_sum/max/min_f32 plus reduce_sum_f64 with every lane

Verified on an RTX 5090. Related to #390: max/min currently lower through libdevice, so these helpers speed up automatically when that lands.

Merging.

@nihalpasham
nihalpasham merged commit 91a33bd into NVIDIA:main Jul 27, 2026
33 checks passed
@honeyspoon
honeyspoon deleted the up/warp-reduce-f32 branch July 28, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request intrinsics Device intrinsics and libdevice math mappings

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants