Repository navigation
Reduce over the live lanes of a partial warp - #672
Merged
nihalpasham merged 2 commits intoAug 6, 2026
Merged
Conversation
The warp reductions shuffle with the full 32-lane member mask, so every lane must be launched and converged. A block whose width is not a multiple of 32 leaves its last warp short: 48 threads give a second warp of 16 live lanes, and the PTX ISA makes `shfl.sync` undefined when a thread sources a lane that is inactive or outside the member mask. The library offered nothing for that case, so NVIDIA#655 carried a live-lane butterfly private to the `warp_sums` example, correct only for a power-of-two tail by its own admission. Add `reduce_{sum,max,min}_{f32,f64}_partial`, taking the live-lane count, and `live_lanes_1d` to derive it for a one-dimensional block. A power-of-two count takes the same butterfly as the full-warp form with the mask and the first offset cut to the live lanes, which keeps `lane ^ offset` inside the mask at every step. Any other count folds the upper part of the span into the lower half and halves the span, reaching an arbitrary count in `ceil(log2(live))` steps. Every shuffle there names a source below the live count, clamped to the last live lane where the arithmetic would run past it, so no thread sources a lane that was never launched; the clamped result is discarded wherever the source lies outside the current span. One broadcast then leaves the total in every lane, as the full-warp form does. `warp_sums` now calls the library form and drops its private helper. Tests: a host model of the fold, which totals correctly for every live count from 1 to 32 and never names a source at or above the live count; mask tests; and `partial_warp_reduce` on GPU over a 16-lane tail from 48-thread blocks and a 13-lane tail from 45-thread blocks, sum and maximum, exact against the host reference. One measurement belongs in the record. Substituting the full-warp form into those kernels also matches the reference exactly on sm_120, so this hardware returns something harmless for a shuffle sourcing a never-launched lane. The ISA promises nothing about that value on any architecture, which is the reason to name the live lanes rather than rely on it. The example's README says so, so nobody reads the passing run as evidence the full-warp form is safe there.
… loop clippy's needless_range_loop, denied by -D warnings, was blocking NVIDIA#672.
Collaborator
|
The lane math checks out end to end:
This gives the library the |
nihalpasham
approved these changes
Aug 6, 2026
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.
A block whose width is not a multiple of 32 leaves its last warp short, and the library offers no reduction that such a warp may call.
What
The warp reductions shuffle with the full 32-lane member mask, so every lane must be launched and converged. Blocks of 48 threads give a second warp of 16 live lanes, and the PTX ISA makes
shfl.syncundefined when a thread sources a lane that is inactive or outside the member mask.#655 met this in the
warp_sumsexample and carried a live-lane butterfly private to it, correct only for a power-of-two tail by its own admission. Any block width whose tail is not a power of two, 45 threads for instance, has nothing to call.How
A power-of-two count takes the same butterfly as the full-warp form with the mask and the first offset cut to the live lanes, which keeps
lane ^ offsetinside the mask at every step.Any other count folds the upper part of the span into the lower half and halves the span, reaching an arbitrary count in
ceil(log2(live))steps. Every shuffle names a source below the live count, clamped to the last live lane where the arithmetic would run past it, so no thread sources a lane that was never launched. The clamped result is discarded wherever the source lies outside the current span. One broadcast then leaves the total in every lane, as the full-warp form does.warp_sumsnow calls the library form and drops its private helper.Testing
On an RTX 5060 (sm_120):
partial_warp_reducecovers a 16-lane tail from 48-thread blocks and a 13-lane tail from 45-thread blocks, for sum and for maximum, exact against the host reference.warp_sumspasses unchanged, safe over raw 1.021.cargo test --workspaceandcargo clippy --workspace --all-targetsclean.One measurement belongs in the record. Substituting the full-warp form into those kernels also matches the reference exactly on sm_120, so this hardware returns something harmless for a shuffle sourcing a never-launched lane. The ISA promises nothing about that value on any architecture, which is the reason to name the live lanes rather than rely on it. The example's README says so, so a passing run is not read as evidence that the full-warp form is safe there.
Follow-up to #655.