KernelInterface: more sub-group communication (shuffles, votes, constant width) #831
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.52) × (1 − Review Confidence (0.50)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 70%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.344
Real PR Conversation Timeline
1 comments
"Review active on #831"
general
weight: 0.35
maleadt
Adds the sub-group primitives that Molly.jl's CUDA kernels use (
ext/MollyCUDAExt.jl), so that they can be written portably on top of KernelInterface. This is a companion to #830 (@groupreduce/@subgroupreduce).New device functions
| Function | Requirement | Molly.jl use |
|---|---|---|
|
shfl(val, lane)|supports_shuffle(backend, T)| rotating j-atom data around the warp in the kernel without a neighbor list ||
shfl_up(val, offset)|supports_shuffle(backend, T)| building block for sub-group scans (its neighbor finder builds a prefix count by hand) ||
shfl_xor(val, mask)|supports_shuffle(backend, T)| butterfly all-reduce (min/max/\|) of tile masks ||
sub_group_any(pred)/sub_group_all(pred)|supports_subgroups| uniform skip of pair evaluations (vote_any_sync) ||
sub_group_ballot(pred)::UInt64|supports_subgroups, width ≤ 64 | interaction bitmasks;count_ones/trailing_zeroson the result |isbitsstructs and tuples field by field (e.g.SVector, Unitful quantities). For such types,supports_shufflechecks their fields. Molly currently gets this by overriding CUDA.jl's internalshfl_recurse.supports_shuffleand the backend implementation notes now cover all four shuffles.Warp size
get_max_sub_group_size()is now required to be a compile-time constant of the generated code. Loops over lanes and shuffle butterflies get specialized for it, which every backend can provide since kernels are already compiled for a fixed width (sub_group_size(backend)). For type-level decisions (e.g. aUInt32vsUInt64mask) the docs point to passingsub_group_size(backend)from the host.POCL
sub_group_shuffle/sub_group_shuffle_xor. Out-of-range lanes give an unspecified value instead of anInexactError.sub_group_any/sub_group_all/sub_group_ballot(cl_khr_subgroups,cl_khr_subgroup_ballot).finish_module!replaces loads of__spirv_BuiltInSubgroupMaxSizewith the width the kernel is compiled for (intel_reqd_sub_group_size), before optimization.Depends on JuliaGPU/OpenCL.jl#526, which adds the votes and unchecked shuffle lanes to SPIRVIntrinsics. Until it's released, this PR takes SPIRVIntrinsics from that branch:
[sources]inProject.toml;[sources];ka-0.10branch.The branch has the LLVM 10 upgrade that
ka-0.10lacks. Before merging, these should be replaced by a compat bound on the release; all of them are markedTODO.Built on the above (fallbacks, backends may override)
Added after a survey of packages that use warp operations: KomaMRI, KernelIntrinsics/KernelForge, AcceleratedKernels#93, ParallelStencil, ClimaCore, IntervalMDP, …
| Function | Fallback |
|---|---|
| shuffles of other primitive types (
Bool,Char, 64-bit types on Metal, …) | shuffled asUInt32words. Backends now have to supportUInt32natively. ||
shfl(val, lane, width),shfl_down/up/xor(val, x, width)| segments ofwidthlanes with CUDA's semantics (reads from outside the segment return the own value), built onshfl||
sub_group_match_any(val)::UInt64| the lanes with the same value (===), found group by group withshfl+sub_group_ballot. CUDA could usematch.any.sync||
sub_group_any/all(pred, width),sub_group_ballot(pred, width),sub_group_match_any(val, width)| votes within segments ofwidthlanes (masks have a bit per lane of the segment), built on the ballot/match of the whole sub-group. Together with the shuffles with awidth, this lets e.g. 32-lane tiles run on 64-wide sub-groups ||
sub_group_reduce(op, val)| ordered tree withshfl_down, then broadcast withshfl. Backends can dispatch ontypeof(op)for native reductions (Metalsimd_sum, SPIR-VGroupNonUniformIAdd, CUDAredux.sync) ||
sub_group_scan(op, val)| inclusive Hillis-Steele scan withshfl_up|The docs now also say how partial sub-groups behave: lanes without a work-item give unspecified shuffle values, and the votes, match, reduce and scan only take the existing work-items into account.
The new tests are in small functions of their own,
subgroup_communication_testsuiteand helpers, with concrete loops. An earlier version iterated over heterogeneous tuples of functions and passed the resulting union-of-singletons value throughKI.@launch, whoseGC.@preservethen hit JuliaLang/julia#63482: on 1.12 and 1.13, codegen emits a nullgc_preserve_beginoperand, and LLVM segfaults. That is fixed on master by #63483, but the backport to 1.12/1.13 is still pending.Guarantees added after porting Molly, KomaMRI and ParallelStencil
Follows from a cross-backend investigation with experiments on CUDA, AMD (wave32 and wave64), PoCL, rusticl and Intel's CPU OpenCL runtime.
W, sub-groups are formed from consecutive work-items, x fastest: sub-group(lin - 1) ÷ W + 1, lane(lin - 1) % W + 1.- This holds on CUDA (documented), AMD, PoCL, rusticl and Intel's CPU runtime. Intel's CPU runtime forms sub-groups per row for other shapes.
- Still to check on Metal and Intel GPUs.
- It lets kernels with e.g.
(32, 8)work-groups (ParallelStencil's default) rely on the layout.shfl_down/shfl_uppast the sub-group width return the work-item's own value, as CUDA's do. That makes them consistent with the shuffles with awidth.- Cost: free on CUDA and Metal, a select on AMD and SPIR-V.
-
shfl_xorrequiresmask < W.sub_group_reducefallback. It is now an orderedshfl_xorbutterfly over the constant width, so it unrolls and needs no broadcast in full sub-groups. KomaMRI measured the previous fallback at ~40% of a reduction-heavy kernel on CUDA. All sub-groups run the same shuffles.sub_group_scanalso loops over the constant width.- PoCL implements sub-group operations with work-group barriers, so all sub-groups of a work-group have to run the same sub-group operations. This is now documented in
POCLBackend.- Until
pocl_standalone_jllincludes the PoCL fix (JuliaPackaging/Yggdrasil#15001), POCL overrides the reduce/scan fallbacks with a loop it compiles correctly in bounds-checked kernels.For backend packages
KernelInterface 0.4 is still unreleased, so this extends its contract. CUDA.jl, AMDGPU.jl, oneAPI.jl, Metal.jl and OpenCL.jl will need to:
shfl,shfl_up,shfl_xor,sub_group_any,sub_group_allandsub_group_ballot;shfl_downoverrides to the primitive types, so that struct shuffles reach the fallback;get_max_sub_group_size(e.g.32 % Ton CUDA).One open question:
sub_group_ballotis required for widths ≤ 64. If a backend can't support ballot, it could get its own capability query instead.Tests
- rotation via
shfl,shfl_uplanes and ashfl_xorbutterfly all-reduce for every supported type- struct shuffles and
supports_shufflefor structs-
any/all/ballotwith several predicate patterns-
get_max_sub_group_sizereturns the width🤖 Generated with Claude Code