Repository navigation
Conversation
|
Documentation preview: https://juliagpu.github.io/KernelAbstractions.jl/previews/PR990/ |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
61dc4fa to
c4a2810
Compare
| 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). |
There was a problem hiding this comment.
| 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). |
| - `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. |
There was a problem hiding this comment.
Maybe just refer to the kernel_function docstring so we don't have to maintain many copies of the same documentation?
| 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. |
There was a problem hiding this comment.
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)
|
This looks like a good change. I left a few comments regarding repetition in the docstrings. |
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.
c4a2810 to
1c2ba83
Compare
Benchmark resultsCompared with 0a9aaa501, the merge base. 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.
All benchmarks
|
Implements item 4 of #810: KernelInterface 0.5 guarantees a sub-group width only to kernels that request one, and
@kernelkernels 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³:
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):
sumof 2²⁶Float323. 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. Othercl_khr_subgroupsdevices have to reportsupports_subgroups = false.The change
A kernel gets a fixed width only if it asks for one:
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.sub_group_size = W, passed tokernel_function,KI.@launchor a kernel's constructor (my_kernel(backend, 256; sub_group_size = W)), the kernel runs at exactlyW:get_max_sub_group_size()returnsW, and the linear layout is relative toW. A width that isn't offered throws anArgumentError.nothingmeans no request.get_max_sub_group_size().supports_subgroupsnow 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 withALLOW_VARYING_SUBGROUP_SIZE.Compatibility
KernelInterface 0.5 is breaking; KernelAbstractions isn't.
Kernelkeeps its four type parameters and stores a requested width in a field, so code that dispatches on or constructsKernel{B, WS, ND, F}keeps working, and kernels launched without a request compile and launch as before. The field does make aKernel16 bytes rather than a zero-size singleton; launches still don't allocate.sub_group_sizesand the keyword, includingsub_group_size = nothing, whichKI.@launchpasses 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.Val(W)loops, and lane arithmetic on the host. Kernels that only readget_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 callssub_group_sizewithout implementing it is AcceleratedKernels, which reaches it through GPUArrays on every GPU back-end. In order:sub_group_sizesand requests it whenKI.sub_group_sizesis defined.AcceleratedKernels = "0.5.14"andKernelInterface = "0.4.1, 0.5", and JLArrays, which forwardssub_group_sizesto POCL.Validation
With the back-ends adapted (implementing
sub_group_sizesand 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)andfindallthrough GPUArrays (with AcceleratedKernels requesting its width) pass on:sub_group_sizes(32, 1, 2, 4, 8, 16, 64)(32,)(32,)(32,)(32, 16, 8)(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
Kernelboxed by dynamic dispatch now takes, and takes as long within noise.