KernelInterface 0.3: a tighter back-end contract #800
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.52) × (1 − Review Confidence (0.64)).
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.17 · 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.289
Real PR Conversation Timeline
1 comments
"Review active on #800"
general
weight: 0.35
maleadt
KernelInterface (KI) is the layer that backends (CUDA.jl, Metal.jl, oneAPI.jl, AMDGPU.jl, OpenCL.jl, and PoCL in this repository) implement, and that KernelAbstractions builds on. Porting those backends to KI 0.2.3 showed that the interface left too much to the individual backends: every backend validated and sized launches itself, and the copies had drifted apart; the same query meant different things on different backends; and the index and sub-group queries were underspecified exactly where the platforms disagree. A kernel written against KI could therefore behave differently depending on where it ran.
KI 0.3 fixes that before the ports merge. It moves launch validation into KI, and writes down the semantics that backends have to implement, so that the testsuite can check them. The design doesn't change: kernels are plain Julia functions, device functions are overlay stubs, and a backend value comes with a task-local device and queue. This is a breaking release without deprecations, since KI has no users outside the backends yet. Each commit is one of the changes below.
KernelInterface owns the launch
Backends used to implement the whole
Kernelcall, including argument validation and work-group sizing. Now KI implements it, and passes validated 3-D sizes to one method a backend implements:The launch semantics are documented on
KI.Kernel: anndrangeis rounded up to whole work-groups and not masked, a zero anywhere launches nothing, and work-group sizes are checked against the device and the compiled kernel. Invalid launches throw before the driver sees them (here on CUDA):(The last one used to reach the backend as a negative global size.) Keywords that KI doesn't know are passed on to
KI.launch, so backend options like CUDA'sstreamorshmemstill reach the driver.Two renames come with this.
KI.@kernelis nowKI.@launch, since it launches a call like@cudadoes, whileKA.@kerneldefines a kernel.numworkgroupsis nownumgroups, matchingget_num_groupsandmax_num_groups.KI.@launchalso evaluates its backend expression once instead of once per argument, and passes keywords it doesn't know tokernel_functionas compiler options.The limit and the recommendation are separate queries
kernel_max_work_group_sizereturned an occupancy recommendation on CUDA, AMDGPU and oneAPI, and the hard limit on Metal, OpenCL and PoCL. On an RTX 5080 it returns 768 for a trivial kernel that can be launched with 1024 threads. Two queries replace it:max_work_group_size(kernel)is the largest work-group the compiled kernel can be launched with. Launches are validated against it.launch_configuration(kernel; nitems, max_work_group_size)is the recommended work-group size for a launch ofnitemswork-items.ndrangelaunches without aworkgroupsizeuse it, and it falls back to the limit. The problem size is passed separately from the bound, so that heuristics like CUDA'sprefer_blocks(more, smaller blocks) don't have to treat the bound as the problem size.max_work_group_dimsandmax_num_groupsare now required. Theirtypemax(Int)fallbacks meant both "unknown" and "unlimited", and #797 needs them to choose a launch.Typed index queries wrap
The index queries take a result type, e.g.
KI.get_global_id(Int32). The docs said the result is "computed inT" and undefined when it doesn't fit, but the backends implemented it asT(x): a checked conversion, which leaves athrow_inexacterrorbranch in every kernel that uses it. The result is now the exact value moduloT, as withx % T. That costs nothing, and it can be tested: aUInt8query over 384 work-items has to wrap, whereT(x)throws.Backends implement the four primitive queries (
get_local_id,get_group_id,get_local_size,get_num_groups).get_global_idandget_global_sizehave fallbacks derived from them for backends without a builtin (CUDA, HIP), and backends that have one (SPIR-V, Metal) should override them.Sub-groups
supports_subgroups(backend)andsupports_shuffle(backend, T)replaceshfl_down_types. The testsuite used to treat a non-empty type list as the flag for sub-group support.sub_group_size(backend)is a guarantee instead of "a reasonable size": kernels fromkernel_functionrun with exactly that width, so host code can pick aVal(N)for a warp-level reduction. A backend that can't guarantee it reports no sub-group support; PoCL now fixes the width at compile time.get_sub_group_size()counts the work-items that are present, andget_num_sub_groups()iscld(items, width). AMDGPU used÷, reporting 0 sub-groups for a 32-item group on wave64.(sub-group id, lane)pair that doesn't change during the kernel. AMDGPU usedactivelane(), which renumbers lanes under divergence.shfl_downfrom a lane that doesn't exist returns an unspecified value (CUDA returns the caller's own value, OpenCL leaves it undefined), and shuffles are not memory fences.Int. They returnedUInt32on most backends andInt32on CUDA.Execution, devices and memory
kernel_functionhas to store the backend value it was given, so that its options apply (oneAPI dropped its compiler options, and OpenCL its platform, by constructing a new default). A kernel launched after switching devices either works or throws; it never runs on the wrong device.device(backend, A), the device that ownsA. The device functions are required for backends with more than one device, and the single-device fallbacks now throw whenndevices > 1instead of answering for the wrong device.supports_float64andsupports_atomicsnow default tofalse, so that a missing method never claims support.copyto!(backend, dst, src)copies in queue order, returnsdst, and throws anArgumentErrorfor arrays of different lengths. CUDA's implementation copiedlength(dst)elements frompointer(src)without a check.localmemoryandbarrierdocument what memory is shared and which writes become visible.unsafe_free!is an optional hint with a no-op fallback.The contract
The docs now have a "Semantics" section and a contract table that lists, for each area, the required methods and the optional ones with their fallbacks. The public API is declared with
public(Julia 1.11+). There is also a versioning rule: required methods only change in breaking releases, optional methods can be added in any release if their fallback is conservative, and patch releases only add tests for behavior that was already specified. (0.2.3 broke backend CI in a patch release by adding unconditional tests for new obligations.)The testsuite's entry point is now
Testsuite.testsuite(backend, AT), which takes a backend value, so that backends with options can be tested. It checks that the methods without a fallback are implemented, and covers launch validation, non-square launches,copyto!, local memory, barriers, typed-index wrapping, partial sub-groups and shuffles. Two existing launch tests had formulas that only worked because the group count equaled the group size; the tests now use different sizes in every dimension.For KernelAbstractions users
KernelAbstractions.GPUis removed. It didn't mean GPU hardware: KA'sCPUbackend is PoCL and subtyped it, soCPU <: GPU. Backends subtypeKernelAbstractions.Backend, and code that dispatched on::GPUshould dispatch on::Backend, on concrete backend types, or on a capability query. This is in the 0.10 changelog.Backend ports
Each backend's KI PR has commits porting it to 0.3 (they get KI 0.3 from this branch through
[sources]until it's registered):| Backend | PR | KI testsuite |
|---|---|---|
| PoCL | this PR | passes |
| CUDA.jl | JuliaGPU/CUDA.jl#3246 | passes, RTX 5080 and CI |
| Metal.jl | JuliaGPU/Metal.jl#917 | passes, M1 (Julia 1.10 and 1.12) |
| oneAPI.jl | JuliaGPU/oneAPI.jl#624 | port in progress |
| AMDGPU.jl | JuliaGPU/AMDGPU.jl#1047 | port in progress |
| OpenCL.jl | JuliaGPU/OpenCL.jl#474 | passes, PoCL and NVIDIA (no sub-groups on NVIDIA: the width can't be fixed) |
Host launch overhead on CUDA (RTX 5080, 1024-element kernel, per launch) is unchanged, except that launches with an explicit size now also query the kernel's thread limit (about 36 ns):
| | KI 0.2.3 | KI 0.3 |
|---|---|---|
|
@cuda| 1.68 µs, 256 B | 1.68 µs, 256 B ||
KI.@launch, explicit sizes | 1.85 µs, 416 B | 1.95 µs, 416 B ||
KI.@launch,ndrange| 2.03 µs, 416 B | 2.04–2.11 µs, 416 B || compiled
Kernel, explicit sizes | 1.42 µs, 160 B | 1.49–1.51 µs, 160 B || compiled
Kernel,ndrange| 1.66 µs, 160 B | 1.70–1.74 µs, 160 B |KA's Buildkite jobs test the backends' KA 0.10 branches, which still require KI 0.2.3, so they can't be installed together with this PR and soft-fail until those branches are rebased onto the ported KI PRs. The ports above were tested against this branch by hand.
Not in this PR
Dynamic local memory, fences and barrier scopes,
max_local_memory, more collectives and sub-group size requests can all be added in a minor release; launch options like dynamic local memory can becomeKernelkeywords without changingKI.launch. #797 and #801, stacked on this PR, use the new contract to launch@kernelkernels on N-d grids with 32-bit indices, and to replace the launch code that every backend copies with one generic implementation in KA.