Skip to content

Reduce the host-side cost of a kernel launch - #392

Open
maleadt wants to merge 2 commits into
mainfrom
tb/launch-overhead
Open

maleadt wants to merge 2 commits into
mainfrom
tb/launch-overhead

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member

A cuTile launch cost ~4.6 µs and 2.4 KB of host time for a two-array kernel, and ~9 µs for nine arrays, against ~1.5 µs for a CUDA.jl @cuda launch. Short kernels were host-bound, and the autotuner (#389) needs to look up the kernels a launch would use on every call.

Two changes:

  • Argument conversion. The TileArray type depends on run-time facts: the index type, and the alignment, contiguity and divisibility that make up its ArraySpec. Building that type and calling it cost ~550 ns per array. The index type now selects one of two type-stable paths, and the type is memoized by the layout facts, as a Val to dispatch on rather than a type to call: ~60 ns per array.
  • Kernel lookup. Every launch looked its kernel up from scratch: Base.method_instance, the job, the CodeInstance holding the results, a lock and the per-context scan (~1.6 µs). cufunction now puts GPUCompiler.cached_lookup in front of that, as CUDACore's does, keyed on the context, the options and the world: ~0.1 µs on a hit. Kernels are still looked up in the latest world, so a redefinition shows on the next launch even from running code. cufunction specializes on the function and argument types for this; the compiler behind it still doesn't.

RTX 5080, Julia 1.13, three runs each (launch=false is the @cuda call without the launch; conversion is cuTileconvert of every array):

2 arrays, before after 9 arrays, before after
@cuda launch 4.61–4.64 µs, 2464 B 2.42–2.45 µs, 1104 B 9.09–9.49 µs, 6784 B 3.10–3.14 µs, 3216 B
@cuda launch=false 3.02–3.12 µs, 2272 B 0.71–0.73 µs, 912 B 7.31–7.70 µs, 6368 B 1.35–1.36 µs, 2800 B
conversion 1.06–1.08 µs, 912 B 0.12 µs, 304 B 4.71–4.98 µs, 3776 B 0.55–0.67 µs, 880–1040 B
CUDA.jl @cuda (reference) 1.43–1.49 µs 1.60–1.67 µs

First launch in a fresh session (warm CUBIN cache, interleaved runs): 0.63–0.66 s → 0.55–0.58 s with two arrays, 0.67–0.74 s → 0.59–0.64 s with nine; a second kernel in the same session is unchanged (60–90 ms). The precompile workload now also compiles the conversion of common array types and the lookup's miss path, which grows the package image from 35.9 to 37.2 MiB; precompilation time is unchanged.

What remains between a cuTile launch and a CUDA.jl one:

  • CUDACore's @cuda pipeline builds its KernelCall and the NamedTuple describing the call's sources at run time when the converted arguments' types aren't inferable, which costs ~0.5 µs per launch. With that fixed in CUDACore, a two-array launch takes ~1.9 µs and launch=false ~0.17 µs.
  • A converted array is a value whose type is only known at run time, so each one is boxed (80 bytes).
  • A kernel takes a pointer, sizes and strides per array, so nine arrays pass 28 parameters, which the driver takes longer to launch.

A TileKernel returned by cufunction is rebuilt around the cached function on every call, so two lookups of one kernel are no longer ===; their fun is. TileKernel.opts now holds the options as given, with sm_arch=nothing meaning the device's. Requires GPUCompiler 2.18.

Converting an array for a launch took ~550 ns: the index type was only
known at run time, so converting the sizes and strides dispatched
dynamically, building the `ArraySpec` type from the layout facts
(alignment, contiguity, divisibility) cost several times the rest, and
calling the resulting type to construct the array cost ~100 ns more.

Branch on the index type, compute the layout facts as plain values, and
look the `TileArray` type up by those facts in a lock-free memo per
element type, rank and index type, as a `Val` to dispatch on rather than
a type to call. The memo keeps the 16 most recent layouts, so that a hit
stays cheap when many are seen. A conversion now takes ~60 ns and
allocates 80 instead of 384 bytes: the result and its arguments, whose
type is only known at run time, are boxed.

The conversion is now compiled per array type rather than inlined into
every kernel's launch code, and precompiled for the common ones, which
keeps the first launch from getting slower.
Every launch looked its kernel up from scratch: the method table lookup
in `Base.method_instance`, the job, the CodeInstance holding the
compiled results, a lock and the scan for the context's function. That
took ~1.6 µs on every launch.

Put `GPUCompiler.cached_lookup` in front of it, as CUDACore's `cufunction`
does, keyed on the context, the options and the world. The world is the
latest one, which the lookup uses, so that a redefinition still shows on
the next launch from code that is already running; `cached_lookup` also
matches the caller's world, which can only cost a miss. To make the
cache a constant per signature, `cufunction` now specializes on the
function and argument types; the compilation behind it still doesn't.
`@cuda` passes the alias grouping as a constant when no arrays overlap,
so that its lookup is resolved statically.

The cache must not keep the kernel function, whose captured values come
from each launch, so a `TileKernel` is rebuilt around the cached
function and its options as given. Two lookups of one kernel share their
`fun`, which the tests now compare, but no longer necessarily the rest.
For the same reason the per-context function cache now holds the
`CuFunction` rather than the first `TileKernel`, which kept the values
captured by that launch's closure alive.

A hit now takes ~0.1 µs. The precompile workload compiles the lookup's
miss path, which is shared by every kernel launched without options, so
the first launch doesn't get slower. `cached_lookup` requires GPUCompiler
2.18.

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.

1 participant