Skip to content

Implement KernelInterface, and support KernelAbstractions 0.10 - #3314

Merged
maleadt merged 9 commits into
mainfrom
ka-0.10
Oct 13, 2026
Merged

maleadt merged 9 commits into
mainfrom
ka-0.10

Conversation

@maleadt

@maleadt maleadt commented Sep 30, 2026

Copy link
Copy Markdown
Member

KernelAbstractions 0.10 moves its back-end API into a separate package, KernelInterface. A back end implements KernelInterface: allocating and copying memory, selecting devices, compiling and launching kernels, and the device-side intrinsics. KernelAbstractions then launches @kernel kernels the same way on every back end (JuliaGPU/KernelAbstractions.jl#801). This PR ports CUDA.jl to that model in one step. It replaces #3246, which added KernelInterface next to the existing KernelAbstractions 0.9 back end, and #3302/#3304, which then ported that to KernelAbstractions 0.10.

CUDABackend now implements KernelInterface, in CUDACore, which depends on KernelInterface instead of KernelAbstractions. That makes the KernelInterface layer usable on its own, without KernelAbstractions' macros:

using CUDA
import KernelInterface as KI

function vadd(c, a, b)
    i = KI.get_global_id().x
    if i <= length(c)
        @inbounds c[i] = a[i] + b[i]
    end
    return
end

a = CUDA.rand(1000); b = CUDA.rand(1000); c = similar(a)
KI.@launch CUDABackend() ndrange=length(c) vadd(c, a, b)

KernelAbstractions becomes a weak dependency. Its extension only provides what KernelAbstractions still needs from a back end: the MArray behind @private, the Adapt rule behind @Const, and the maxthreads hint for kernels with a static workgroup size. CUDA's own copy of the KernelAbstractions launch path goes away: partitioning the ndrange, building the kernel's context, tuning the workgroup size, and calling the kernel. What remains specific to KernelAbstractions is a 37-line extension.

Kernels behave the same, but they are launched differently. KernelAbstractions now launches them on a 3-D grid, and computes @index in 32 bits when the iteration space fits (JuliaGPU/KernelAbstractions.jl#797). So a kernel over a 3-D ndrange doesn't need divisions to compute its index. The test suite checks that for the generated PTX. With a KA kernel that copies a Float32 array, on an RTX 5080:

main (KA 0.9) this PR
256×256×256, @index(Global, Cartesian) 180 µs 161 µs
256×256×256, @index(Global, Linear) 175 µs 149 µs
255×257×129, @index(Global, Cartesian) 86 µs 41 µs
255×257×129, @index(Global, Linear) 80 µs 37 µs
launch of an empty kernel 1.72 µs 1.89 µs
launch of a kernel with 40 arguments 1.95 µs, 336 bytes 1.98 µs, 304 bytes

CUDABackend(; prefer_blocks=true) still prefers more, smaller blocks. It now takes effect in KI.launch_configuration, which receives the number of work-items to cover.

Launching kernels with many arguments stays as cheap as with #3309. KernelInterface 0.4 passes the arguments to the back end as a tuple (JuliaGPU/KernelAbstractions.jl#811), and CUDA forwards that tuple to its own launch. KI.launch rejects threads and blocks, which would override the launch geometry KernelInterface has validated, but passes CUDA's other launch options on:

kernel = KI.@launch CUDABackend() launch=false vadd(c, a, b)
kernel(c, a, b; ndrange=length(c), stream=stream())   # fine
kernel(c, a, b; ndrange=length(c), threads=256)       # ArgumentError

The first commit is the port. The next ones implement parts of KernelInterface that are optional: sub-groups (warps, including a partial last warp), ordering work across tasks with CUDA events for KernelAbstractions.@spawn, and KI.versioninfo.

One regression remains. KernelAbstractions converts the arguments twice, once to determine the argument types to compile for and once when launching, where CUDA converted them once. cudaconvert is supposed to be pure, so this only shows with a conversion that has side effects. Two tests that count conversions are now @test_broken.

This needs a breaking release, since it requires KernelAbstractions 0.10, which isn't registered yet. Until then, the last commit takes KernelAbstractions and KernelInterface from their development branch, through [sources] and, for Julia 1.10 and 1.11, which don't pick those up, by developing them explicitly on Buildkite. That commit is dropped before merging.

@github-actions

github-actions Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

CUDA.jl Benchmarks

Details
Benchmark suite Current: 1d3ed79 Previous: 6baba34 Ratio
array/accumulate/Float32/1d 64480 ns 65161 ns 0.99
array/accumulate/Float32/dims=1 208987 ns 208138 ns 1.00
array/accumulate/Float32/dims=1L 231031 ns 230224 ns 1.00
array/accumulate/Float32/dims=2 93336 ns 94144 ns 0.99
array/accumulate/Float32/dims=2L 4415210 ns 3404076 ns 1.30
array/accumulate/Int64/1d 106085 ns 105158 ns 1.01
array/accumulate/Int64/dims=1 224247 ns 223408 ns 1.00
array/accumulate/Int64/dims=1L 404935 ns 402921 ns 1.00
array/accumulate/Int64/dims=2 112330 ns 110659 ns 1.02
array/accumulate/Int64/dims=2L 5304209 ns 4899488 ns 1.08
array/broadcast 11021 ns 16932 ns 0.65
array/broadcast launch 7058.25 ns 6902.2 ns 1.02
array/construct 944.4137931034483 ns 954.5116279069767 ns 0.99
array/copy 17098 ns 16941 ns 1.01
array/copyto!/cpu_to_gpu 207099 ns 206485 ns 1.00
array/copyto!/gpu_to_cpu 240664 ns 240670 ns 1.00
array/copyto!/gpu_to_gpu 8902 ns 8848 ns 1.01
array/iteration/findall/bool 48972 ns 45361 ns 1.08
array/iteration/findall/int 70604 ns 67387 ns 1.05
array/iteration/findfirst/bool 115284 ns 113427 ns 1.02
array/iteration/findfirst/int 112789 ns 110746 ns 1.02
array/iteration/findmin/1d 66312 ns 66055 ns 1.00
array/iteration/findmin/2d 66992 ns 66143 ns 1.01
array/iteration/logical 52956 ns 47895 ns 1.11
array/iteration/scalar 62096 ns 59557 ns 1.04
array/permutedims/2d 47944 ns 48953 ns 0.98
array/permutedims/3d 45945 ns 50214 ns 0.91
array/permutedims/4d 48729 ns 51736 ns 0.94
array/random/rand/Float32 11237 ns 10898 ns 1.03
array/random/rand/Int64 18464 ns 18012 ns 1.03
array/random/rand!/Float32 8370.666666666666 ns 8228.666666666666 ns 1.02
array/random/rand!/Int64 16115 ns 15944 ns 1.01
array/random/randn/Float32 34350 ns 34518 ns 1.00
array/random/randn!/Float32 25240 ns 24784 ns 1.02
array/reductions/mapreduce/Float32/1d 48881 ns 48394 ns 1.01
array/reductions/mapreduce/Float32/dims=1 42500 ns 41816 ns 1.02
array/reductions/mapreduce/Float32/dims=1L 71802 ns 72029 ns 1.00
array/reductions/mapreduce/Float32/dims=2 45221 ns 43634 ns 1.04
array/reductions/mapreduce/Float32/dims=2L 73659 ns 72768 ns 1.01
array/reductions/mapreduce/Int64/1d 50332 ns 49524 ns 1.02
array/reductions/mapreduce/Int64/dims=1 44352 ns 44020 ns 1.01
array/reductions/mapreduce/Int64/dims=1L 112318 ns 111339 ns 1.01
array/reductions/mapreduce/Int64/dims=2 45131 ns 44454 ns 1.02
array/reductions/mapreduce/Int64/dims=2L 99223 ns 97947 ns 1.01
array/reductions/reduce/Float32/1d 49690 ns 48162 ns 1.03
array/reductions/reduce/Float32/dims=1 43125 ns 43092 ns 1.00
array/reductions/reduce/Float32/dims=1L 72730 ns 72451 ns 1.00
array/reductions/reduce/Float32/dims=2 45101 ns 44144 ns 1.02
array/reductions/reduce/Float32/dims=2L 73551 ns 73201 ns 1.00
array/reductions/reduce/Int64/1d 50482 ns 49775 ns 1.01
array/reductions/reduce/Int64/dims=1 45208 ns 44003 ns 1.03
array/reductions/reduce/Int64/dims=1L 112782 ns 111722 ns 1.01
array/reductions/reduce/Int64/dims=2 45286 ns 44349 ns 1.02
array/reductions/reduce/Int64/dims=2L 100194 ns 99044 ns 1.01
array/reverse/1d 24097 ns 23639 ns 1.02
array/reverse/1dL 129768 ns 129433 ns 1.00
array/reverse/1dL_inplace 71315 ns 71036 ns 1.00
array/reverse/1d_inplace 13062 ns 12884 ns 1.01
array/reverse/2d 23309 ns 22975 ns 1.01
array/reverse/2dL 81880 ns 81369 ns 1.01
array/reverse/2dL_inplace 71610 ns 71387 ns 1.00
array/reverse/2d_inplace 15517 ns 15438 ns 1.01
array/sorting/1d 752558 ns 723576 ns 1.04
array/sorting/2d 195807 ns 203045 ns 0.96
array/sorting/by 929828 ns 902398 ns 1.03
cuda/graph/capture 42220 ns 42336 ns 1.00
cuda/graph/eager 39701 ns 37982 ns 1.05
cuda/graph/launch 13969 ns 14215 ns 0.98
cuda/synchronization/context/auto 7765.666666666667 ns 7864.333333333333 ns 0.99
cuda/synchronization/context/blocking 906.0512820512821 ns 897.0227272727273 ns 1.01
cuda/synchronization/context/nonblocking 7844.666666666667 ns 7834 ns 1.00
cuda/synchronization/stream/auto 884.8510638297872 ns 825.3116883116883 ns 1.07
cuda/synchronization/stream/blocking 762.6304347826087 ns 763.1478260869566 ns 1.00
cuda/synchronization/stream/nonblocking 7744.333333333333 ns 7732.25 ns 1.00
integration/byval/reference 147628 ns 147654 ns 1.00
integration/byval/slices=1 148829 ns 148956 ns 1.00
integration/byval/slices=2 290457 ns 290277 ns 1.00
integration/byval/slices=3 432480 ns 432483 ns 1.00
integration/cudadevrt 105059 ns 105162 ns 1.00
integration/volumerhs 9610564 ns 9610718 ns 1.00
kernel/indexing 11774 ns 11654 ns 1.01
kernel/indexing_checked 12292 ns 12087 ns 1.02
kernel/launch 2515.222222222222 ns 2303.222222222222 ns 1.09
kernel/occupancy 908.2432432432432 ns 856.9508196721312 ns 1.06
kernel/rand 15294 ns 15025 ns 1.02
latency/import 4752123716 ns 4228554170 ns 1.12
latency/precompile 5386305022 ns 5159830519 ns 1.04
latency/ttfp 5243134821 ns 4917633017 ns 1.07

This comment was automatically generated by workflow using github-action-benchmark.

maleadt and others added 9 commits October 13, 2026 06:36
KernelAbstractions 0.10 builds on KernelInterface, which defines what a back
end provides: memory and device management, compiling and launching kernels,
and the device-side intrinsics. KernelAbstractions then launches `@kernel`
kernels itself on any KernelInterface back end, which replaces CUDA's copy of
that launch path: partitioning the ndrange, building the kernel's context, and
tuning the workgroup size.

`CUDABackend` now implements KernelInterface in CUDACore, which depends on it
instead of on KernelAbstractions. What KernelAbstractions still needs from a
back end moves to an extension: the `MArray` behind `@private`, the Adapt rule
for `@Const`, and the `maxthreads` hint for a static workgroup size.
`prefer_blocks` now applies in `KI.launch_configuration`, which receives the
number of work-items.

`KI.launch` passes the kernel arguments on as a tuple, so kernels with many
arguments stay cheap to launch, as #3309 made them for the old launch path. It
rejects `threads` and `blocks`, which would override the launch geometry that
KernelInterface validated. `KI.copyto!` accepts dense arrays and contiguous
views of them, as the old `KA.copyto!` did.

KernelAbstractions converts the arguments twice, to determine the types to
compile for and again when launching, where CUDA's launch converted them once.
`cudaconvert` has to be pure, so this only shows with conversions that have
side effects, as in two tests that count them.

Co-authored-by: Christian Guinard <28689358+christiangnrd@users.noreply.github.com>
Sub-groups are warps: implement the sub-group queries, `sub_group_barrier` and
`shfl_down`, and report their support to KernelInterface. KernelInterface
leaves unspecified how work-items are grouped into sub-groups; CUDA forms warps
from consecutive linear thread indices, so the last warp of a block can be
partial, which a test checks.

Co-authored-by: Christian Guinard <28689358+christiangnrd@users.noreply.github.com>
Implement `KI.record_event` and `KI.wait_event` with a `CuEvent` recorded on,
and waited for by, the task's stream. `KernelAbstractions.@spawn` uses them to
order a new task's work after the work its parent had queued, without
synchronizing the parent; the default is a full synchronization.

KernelInterface's testsuite checks them once `record_event` returns an event.
`KI.versioninfo(CUDABackend())` prints `CUDA.versioninfo()`.
KernelAbstractions now backs @Private with a stack-allocated PrivateArray,
provided as an overlay in GPUCompiler's shared method table, which CUDA's
method table already stacks on top of. Drop our MArray-based Scratchpad
override so that it takes effect, and require GPUCompiler 2.12.

That leaves StaticArrays only for the dimension_mismatch_fail device
override, so make it a weak dependency with a regular extension.
KI.max_work_group_size and KI.launch_configuration are specialized on the
kernel, so their bodies were compiled for every kernel launched through
KernelAbstractions. Forward them to functions of the CuFunction, which are
compiled once.
Implement the sub-group communication of KernelInterface's updated contract: `shfl`,
`shfl_up` and `shfl_xor` next to `shfl_down`, their variants within segments of a `width`
(a single `shfl.sync` each), and the votes `sub_group_any`, `sub_group_all` and
`sub_group_ballot`, using the warp intrinsics with the full mask. Warps are formed from
consecutive linear thread indices and are independent, so report
`supports_linear_subgroups` and `supports_independent_subgroups`.

The shuffles, and `supports_shuffle`, are restricted to the primitive types CUDA's warp
shuffles support, so that KernelInterface shuffles structs and tuples (including `Complex`)
field by field. `shfl.sync` only uses the low 5 bits of an offset, so offsets past the warp
(including ones that don't fit in 32 bits) become 0, giving the thread's own value as
KernelInterface requires.

The warp size is the constant 32 rather than a read of `%WARP_SZ`, so that
`get_max_sub_group_size` is a constant of the generated code, and the sub-group queries are
computed with unsigned 32-bit shifts.

Co-authored-by: Tim Besard <tim@juliahub.com>
On sm_80 and later, implement `sub_group_reduce` of `+`, `min`, `max`, `&`, `|` and `⊻` on
32-bit integers with a single `redux.sync`. These operators are commutative, so the order of
the lanes, which KernelInterface's reduction follows, doesn't change the result. Other
operators, types and devices use KernelInterface's fallback.

Co-authored-by: Tim Besard <tim@juliahub.com>
Report `supports_partitionable_subgroups`: the functions with a `width` now pass the member
mask of the caller's segment to the `.sync` warp intrinsics instead of the full mask, so that
only the threads of that segment have to execute them together. This covers the width shuffles,
the votes (masking other segments out of the ballot and shifting it to the segment), the
32-bit integer reductions with `redux.sync`, and the width `sub_group_barrier`, which becomes
`bar.warp.sync` on the segment. Other reductions and scans use KernelInterface's fallbacks on
top of the width shuffles.

The width is a constant, so a width of 32 still compiles to the full mask. This lets
KernelAbstractions put several narrow tiles in one warp: a DG-like kernel with tiles of 16
runs 2.4x faster with 16 tiles per block than with a block per tile.
@maleadt
maleadt merged commit 212af1e into main Oct 13, 2026
14 of 16 checks passed
@maleadt
maleadt deleted the ka-0.10 branch October 13, 2026 07:37
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.14706% with 27 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.08%. Comparing base (6baba34) to head (1d3ed79).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
CUDACore/src/CUDAKernels.jl 78.86% 26 Missing ⚠️
CUDACore/ext/KernelAbstractionsExt.jl 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3314      +/-   ##
==========================================
- Coverage   87.07%   85.08%   -2.00%     
==========================================
  Files         191      191              
  Lines       19472    19242     -230     
==========================================
- Hits        16956    16372     -584     
- Misses       2516     2870     +354     

☔ 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 added a commit that referenced this pull request Oct 14, 2026
…3391)

Since KernelAbstractions 0.10 (#3314) this step takes 7-10 minutes longer when the precompile cache is cold: KA 0.10 itself precompiles much slower, core/exceptions recompiles the stack at two extra debug levels, and the KA/KernelInterface tests grew. It was already at 17-25 minutes, so it now hits the 30-minute limit. Match the other memory-configuration steps.
SimonDanisch added a commit to SimonDanisch/CUDA.jl that referenced this pull request Oct 16, 2026
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.

3 participants