Skip to content

KernelInterface 0.5: only guarantee a sub-group width to kernels that request one - #990

Open
maleadt wants to merge 2 commits into
mainfrom
tb/sub-group-request
Open

maleadt wants to merge 2 commits into
mainfrom
tb/sub-group-request

Conversation

@maleadt

@maleadt maleadt commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Implements item 4 of #810: KernelInterface 0.5 guarantees a sub-group width only to kernels that request one, and @kernel kernels get a keyword to request it.

The problem

KernelInterface 0.4 promises that every kernel runs at the width KI.sub_group_size(backend) returns. Most kernels don't need that: they use no sub-group operations, or read the width on the device. But back-ends have to pin the width for all of them, and that has three costs.

1. Intel back-ends force SIMD32 on every kernel, which makes register-heavy kernels spill (JuliaGPU/oneAPI.jl#772). Here is the register-blocked matmul from JuliaGPU/GPUArrays.jl#856, at 1024³:

SIMD32 (forced) SIMD16
Iris Xe, Float32 8.3 ms (1.9 KB spill) 4.9 ms
Iris Xe, ComplexF32 82 ms (5.6 KB spill) 15 ms
Arc A770, Float32 0.99 ms (1.2 KB spill) 0.61 ms
Arc A770, ComplexF32 6.8 ms (4.8 KB spill) 1.35 ms

Forcing 16 instead is no fix: kernels that don't spill are faster at 32. GPUArrays' tiled matmul, for example, runs about 15% slower at 16 on Iris Xe (119 vs 104 ms). The right width depends on the kernel, and only the back-end can tell which one that is. JuliaGPU/oneAPI.jl#773 works around the promise. For kernels whose SPIR-V can't observe the width, it switches to SIMD16 when SIMD32 spills. That needs a fail-closed scan of every spilling kernel, and a gap in that scan means wrong results.

2. Kernels that depend on the width can't choose it. AcceleratedKernels' reductions bake the width into the kernel and size local memory with it. Their best width differs by GPU (minimum of 3 runs):

sum of 2²⁶ Float32 width 32 width 16
Arc A770 1.81 ms 1.04 ms
Iris Xe 14.4 ms 16.1 ms

3. Devices without a way to require a width get no sub-groups at all. OpenCL.jl only enables sub-groups on devices with cl_intel_required_subgroup_size. Other cl_khr_subgroups devices have to report supports_subgroups = false.

The change

A kernel gets a fixed width only if it asks for one:

# 0.4: every kernel runs at KI.sub_group_size(backend)
W = KI.sub_group_size(backend)
KI.@launch backend reduce_kernel(out, a, Val(W))

# 0.5: request the width the kernel is written for
sizes = KI.sub_group_sizes(backend)
isempty(sizes) && return reduce_without_subgroups(out, a)
W = first(sizes)                        # or a width tuned for this device
KI.@launch backend sub_group_size=W reduce_kernel(out, a, Val(W))

# the same through KernelAbstractions
reduce_kernel(backend; sub_group_size = W)(out, a, Val(W); ndrange)
  • KI.sub_group_sizes(backend) lists the widths a kernel can request. The first is the back-end's suggestion for code that needs a fixed width and has no reason to pick another; the order of the rest means nothing. The fallback is (), which is also what a back-end with sub-groups but no way to require a width returns.
  • With sub_group_size = W, passed to kernel_function, KI.@launch or a kernel's constructor (my_kernel(backend, 256; sub_group_size = W)), the kernel runs at exactly W: get_max_sub_group_size() returns W, and the linear layout is relative to W. A width that isn't offered throws an ArgumentError. nothing means no request.
  • Without a request, the back-end picks the width for each launch, and the kernel reads it with get_max_sub_group_size().
  • supports_subgroups now only means that the sub-group operations work.
  • KI.sub_group_size(backend) throws an error that explains how to migrate. It goes away in 1.0. Code that sized buffers from it now fails loudly instead of computing wrong results: in an experiment on oneAPI.jl with the pin removed, AcceleratedKernels summed 1000 ones to 256.

This is the model of SYCL (reqd_sub_group_size, info::device::sub_group_sizes), WGSL (@subgroup_size) and HLSL ([WaveSize]). Vulkan 1.3 has the same request (requiredSubgroupSize), and lets the width vary for SPIR-V 1.6 modules or with ALLOW_VARYING_SUBGROUP_SIZE.

Compatibility

KernelInterface 0.5 is breaking; KernelAbstractions isn't. Kernel keeps its four type parameters and stores a requested width in a field, so code that dispatches on or constructs Kernel{B, WS, ND, F} keeps working, and kernels launched without a request compile and launch as before. The field does make a Kernel 16 bytes rather than a zero-size singleton; launches still don't allocate.

  • Back-ends implement sub_group_sizes and the keyword, including sub_group_size = nothing, which KI.@launch passes along. CUDA and Metal offer (32,), AMDGPU its wavefront size; they reject other widths. Intel back-ends offer their widths up to 64, and can stop pinning unrequested kernels. KernelAbstractions' POCL back-end is updated here.
  • Kernels that depend on the width have to request it: shared-memory layouts, Val(W) loops, and lane arithmetic on the host. Kernels that only read get_max_sub_group_size() on the device, like KernelAbstractions' @groupreduce, need no change.

Rollout

No compat caps in General are needed. Every back-end requires KernelInterface = "0.4" today, so this release isn't picked up until they are updated. The only registered package that calls sub_group_size without implementing it is AcceleratedKernels, which reaches it through GPUArrays on every GPU back-end. In order:

  1. AcceleratedKernels 0.5.14, which picks a width from sub_group_sizes and requests it when KI.sub_group_sizes is defined.
  2. KernelInterface 0.5.0 and KernelAbstractions 0.10.9 (this PR).
  3. GPUArrays, with AcceleratedKernels = "0.5.14" and KernelInterface = "0.4.1, 0.5", and JLArrays, which forwards sub_group_sizes to POCL.
  4. The back-ends (CUDA.jl, AMDGPU.jl, Metal.jl, oneAPI.jl, OpenCL.jl), each requiring KernelInterface 0.5, KernelAbstractions 0.10.9 and that GPUArrays. That last bound keeps AcceleratedKernels 0.5.13 away from back-ends that no longer pin the width.

Validation

With the back-ends adapted (implementing sub_group_sizes and the keyword; branches to follow), the KernelInterface testsuite, KernelAbstractions' launch, host-interface and group-reduction testsuites, and AcceleratedKernels' sum, cumsum, findmax/findmin, sum(dims), cumsum(dims) and findall through GPUArrays (with AcceleratedKernels requesting its width) pass on:

Back-end Device sub_group_sizes
POCL CPU (32, 1, 2, 4, 8, 16, 64)
CUDA RTX 5080 (32,)
AMDGPU Ryzen 9950X iGPU (gfx1036) (32,)
Metal M1 (32,)
oneAPI Iris Xe, Arc A770 (32, 16, 8)
OpenCL Arc A770 (32, 16, 8)

On Metal, KernelAbstractions' "many arguments" test fails as it does on main (Metal's 31-buffer limit; Metal.jl skips it).

oneAPI is the back-end whose width varies: unrequested kernels are compiled for SIMD32, or SIMD16 if they spill at 32, without JuliaGPU/oneAPI.jl#773's scan of their SPIR-V. Its tests check that a spilling kernel that reads its width gets and observes 16, and that the same kernel requesting 32 keeps it.

Launching without a request doesn't allocate, including through a handle whose type isn't known statically, and takes as long as on main. In Oceananigans (CliMA/Oceananigans.jl#5799, the port to KernelAbstractions 0.10), a time step of the 48×48×8 models from its allocation tests makes as many allocations as on main, on the RTX 5080 and on the CPU; on the GPU, a hydrostatic step allocates 96 more bytes (184 704 vs 184 608), from the 16 bytes a Kernel boxed by dynamic dispatch now takes, and takes as long within noise.

@github-actions

Copy link
Copy Markdown
Contributor

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.92%. Comparing base (0a9aaa5) to head (1c2ba83).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/backend_launch.jl 85.71% 1 Missing ⚠️
src/macros.jl 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #990      +/-   ##
==========================================
- Coverage   89.99%   88.92%   -1.07%     
==========================================
  Files          33       33              
  Lines        3059     3035      -24     
==========================================
- Hits         2753     2699      -54     
- Misses        306      336      +30     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt force-pushed the tb/sub-group-request branch 2 times, most recently from 61dc4fa to c4a2810 Compare November 20, 2026 10:37
@maleadt
maleadt marked this pull request as ready for review November 20, 2026 11:00
Comment on lines +726 to +729
For a `width` smaller than the sub-group width, they are built on [`sub_group_ballot`](@ref),
and so need a sub-group width of at most 64; with a wider sub-group, their results are
unspecified. Every width in [`sub_group_sizes`](@ref) is at most 64; a kernel compiled without
a requested width can check [`get_max_sub_group_size`](@ref).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
For a `width` smaller than the sub-group width, they are built on [`sub_group_ballot`](@ref),
and so need a sub-group width of at most 64; with a wider sub-group, their results are
unspecified. Every width in [`sub_group_sizes`](@ref) is at most 64; a kernel compiled without
a requested width can check [`get_max_sub_group_size`](@ref).
For a `width` smaller than the sub-group width, they need a sub-group width of at most 64.
A kernel compiled without a requested width can check [`get_max_sub_group_size`](@ref).

Comment on lines +530 to +533
- `name`, `sub_group_size` and any other keyword are passed to [`kernel_function`](@ref)
as compiler options. Pass `sub_group_size = W`, for a `W` in
[`sub_group_sizes(backend)`](@ref sub_group_sizes), when the kernel depends on its
sub-group width being `W`, or `sub_group_size = nothing` for no request.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just refer to the kernel_function docstring so we don't have to maintain many copies of the same documentation?

Comment on lines +487 to +498
options it carries apply to the launch.

Every backend must accept `sub_group_size = nothing` and treat it like a missing
keyword. If [`sub_group_sizes(backend)`](@ref sub_group_sizes) isn't empty, the backend
must also implement the keyword for widths: a kernel compiled with `sub_group_size = W`
executes with exactly width `W`. It throws an `ArgumentError` for a width that isn't in
`sub_group_sizes(backend)`, rather than compile the kernel without the guarantee. A
kernel can still fail to compile for an offered width, e.g. because it uses a feature
that isn't supported at that width; the backend then throws as well. KernelInterface can't check this
for the backend: callers reach the backend's `kernel_function` method directly. A kernel
compiled without a request may execute with any width the backend chooses, per launch,
as long as it is the same for all work-items of the launch.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is defined twice in the same docstring, maybe one could be cut down and send the reader up (or down if we keep the details here)

@christiangnrd

Copy link
Copy Markdown
Member

This looks like a good change. I left a few comments regarding repetition in the docstrings.
I think it would also be good to split up the KI 0.5 change and its tests and the pocl backend implementation and its tests

KernelInterface 0.4 promised that every kernel runs with the width
`KI.sub_group_size(backend)` reports. Intel back-ends therefore force 32 on every kernel,
which makes register-heavy kernels spill (JuliaGPU/oneAPI.jl#772), back-ends that can't fix
the width (OpenCL on NVIDIA) have to report no sub-groups at all, and callers that size
things from the width can't choose it.

KernelInterface 0.5 splits the contract. `supports_subgroups` only promises the sub-group
operations; a kernel compiled without a request runs at a width the back-end chooses per
launch, which its work-items read with `get_max_sub_group_size()`. `KI.sub_group_sizes(backend)`
lists the widths of at most 64 that a kernel can request, the back-end's suggestion first;
`kernel_function(...; sub_group_size = W)`, also through `KI.@launch`, compiles a kernel that
runs with exactly `W` and rejects other widths with an `ArgumentError`; `nothing` means no
request, so that callers with an optional width need one call site. The fallback of
`sub_group_sizes` is `()`, which is also right for a back-end with sub-groups that can't
guarantee a width. `KI.sub_group_size(backend)` now throws an error pointing to the
replacements, until it is removed in 1.0, so that code written for 0.4 fails instead of
computing wrong results with a width the kernel doesn't have.

The testsuite runs kernels without a request against the width they report, and the
sub-group queries for every offered width, the communication functions for the first,
narrowest and widest. The POCL back-end offers the device's widths of up to 64, compiling
unrequested kernels for 32, 64 or 16 as before.
With KernelInterface 0.5, only kernels that request a width get a fixed one, and a `@kernel`
had no way to ask: the only route to compiler options is the back-end's `compiler_options`
hook. Kernels whose performance depends on the width, e.g. register-heavy ones that spill
at 32 on Intel GPUs, need to pick it.

Kernel constructors take a `sub_group_size` keyword, `my_kernel(backend, 256;
sub_group_size = 16)`. `Kernel` stores it in a field, keeping its type parameters, and
passes it on to `KI.kernel_function` after the back-end's `compiler_options`, so that
back-ends overriding that hook don't drop it. `nothing`, the default, requests no width and
compiles the kernel as before. The constructors become a single method taking the sizes as
varargs: a kernel defined in a local scope with several keyword methods would box their
keyword bodies. This is the sub-group slice of the per-kernel options proposed in #839.
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with 0a9aaa501, the merge base.
The environment differs from the baseline's: KernelInterface 0.4.7 → 0.5.0.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance −0.9% 0 0 32
Latency +3.8% 2 0 13
Applications +3.8% 1 0 5
Benchmark Baseline This PR Change
Latency
pocl/serial/compile/new-kernel 127 ms 147 ms +15.5% 🔴 slower
pocl/serial/compile/localmem-barrier 672 ms 723 ms +7.7% 🔴 slower
Applications
pocl/serial/stencil/timestep 9.92 ms 10.5 ms +6.3% 🔴 slower
All benchmarks
Benchmark Baseline This PR Change
Performance
julia/serial/arithmetic/muladd-reference 655 µs 655 µs +0.0%
julia/serial/math/exp-f64-reference 284 µs 289 µs +1.9%
julia/serial/narrow/masked-reference 736 µs 708 µs −3.7%
pocl/parallel/atomics/histogram 5.26 ms 5.24 ms −0.3%
pocl/parallel/groupreduce/add-f64 1.09 ms 1.09 ms −0.0%
pocl/parallel/groupreduce/fallback 4.38 ms 4.39 ms +0.1%
pocl/parallel/groupscan/add-i32 393 µs 392 µs −0.3%
pocl/parallel/indexing/cartesian3d 40.9 µs 41.5 µs +1.5%
pocl/parallel/indexing/linear3d 40.5 µs 39.8 µs −1.9%
pocl/parallel/indexing/ntuple3d 39.9 µs 39.3 µs −1.4%
pocl/parallel/indexing/ntuple4d 128 µs 128 µs −0.2%
pocl/parallel/localmem/transpose-naive 436 µs 437 µs +0.3%
pocl/parallel/localmem/transpose-tiled 64.3 µs 66.3 µs +3.1%
pocl/parallel/private/across-barrier 42.3 µs 42.3 µs +0.1%
pocl/parallel/scheduling/default 117 µs 117 µs −0.0%
pocl/parallel/scheduling/explicit 117 µs 117 µs −0.0%
pocl/parallel/stream/copy/cache 630 ns 659 ns +4.6%
pocl/parallel/stream/copy/dram 6.57 ms 6.58 ms +0.1%
pocl/parallel/stream/triad/cache 1.26 µs 1.13 µs −10.7%
pocl/parallel/stream/triad/dram 9.68 ms 9.62 ms −0.7%
pocl/parallel/util/copyto-small 27.0 ns 24.0 ns −11.1%
pocl/parallel/util/zeros-large 931 µs 911 µs −2.1%
pocl/parallel/util/zeros-small 655 ns 627 ns −4.3%
pocl/serial/arithmetic/contract 1.85 ms 1.85 ms +0.0%
pocl/serial/arithmetic/muladd 705 µs 705 µs +0.0%
pocl/serial/const/marked 668 µs 668 µs +0.0%
pocl/serial/const/unmarked 859 µs 860 µs +0.1%
pocl/serial/math/exp-f64 123 µs 124 µs +1.1%
pocl/serial/math/sin-f64 173 µs 168 µs −3.1%
pocl/serial/narrow/masked 635 µs 636 µs +0.1%
pocl/serial/narrow/unmasked 632 µs 635 µs +0.6%
pocl/serial/stream/triad/dram 8.60 ms 8.60 ms +0.0%
Latency
host/import/ka 711 ms 707 ms −0.5%
host/precompile/ka 27.2 s 27.1 s −0.1%
host/startup 128 ms 130 ms +1.6%
pocl/parallel/launch/dynamic-default 165 ns 173 ns +4.8%
pocl/parallel/launch/groupreduce-f64 769 ns 786 ns +2.2%
pocl/parallel/launch/many-args 655 ns 673 ns +2.7%
pocl/parallel/launch/partial-workgroup 234 ns 238 ns +1.7%
pocl/serial/compile/groupreduce 1.49 s 1.56 s +4.7%
pocl/serial/compile/localmem-barrier 672 ms 723 ms +7.7% 🔴 slower
pocl/serial/compile/new-kernel 127 ms 147 ms +15.5% 🔴 slower
pocl/serial/compile/new-workgroupsize 8.60 ms 8.76 ms +1.8%
pocl/serial/first-kernel/cold-cache 406 ms 418 ms +3.1%
pocl/serial/first-kernel/warm-cache 326 ms 341 ms +4.9%
Applications
pocl/parallel/dgsem/fluxdiff-node 244 µs 245 µs +0.6%
pocl/parallel/dgsem/weak-element 90.0 µs 96.5 µs +7.2%
pocl/parallel/dgsem/weak-node-localmem 93.0 µs 93.0 µs +0.1%
pocl/parallel/stencil/timestep 7.19 ms 7.57 ms +5.3%
pocl/serial/stencil/timestep 9.92 ms 10.5 ms +6.3% 🔴 slower

History: https://juliagpu.org/perf/?pkg=kernelabstractions

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants