Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,23 @@ releases](https://github.com/JuliaGPU/cuTile.jl/releases).
command that assembles it, its disassembly, the CUBIN, PTX and SASS. The
bytecode serves as a reproducer for `tileiras` problems; `debuginfo=false`
leaves source locations, which include absolute file paths, out of it.
- `ct.autotune(f, space)` selects the fastest configuration of a launch, such as
its tile sizes and compiler hints, from a search space: it compiles the
configurations concurrently, then times those that compiled.
- `ct.@tuned(space...) do cfg … end` launches with the configuration a tuning
record names, or else the first one; with `search=true` a launch without a
record searches the space first and saves the winner. Records are found by
the code the launch compiles to, are kept per package, and can be shipped
with a package.
- The matmul, batched matmul, fused GEMM, FMHA, MoE and layer-norm examples
choose their tile sizes and hints with `ct.@tuned`, and ship tuning records
for the RTX 5080; `examples/benchmarks.jl --search` tunes the launches that
have no applicable record.

*Minor changes*:

- cuTile.jl requires CUDA.jl 6.6, whose `@cuda` accepts splatted keyword
arguments, such as a configuration's compiler hints.
- `CUDA_Tile_jll` is no longer a dependency. `code_tiled` and
`@device_code_tiled` disassemble with the `tileirdisasm` that ships with CUDA
13.4 and newer, which `CUDA_Compiler_jll` selects by default on any CUDA 13
Expand Down
6 changes: 5 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Scratch = "6c6a2e73-6563-6170-7368-637461726353"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

[weakdeps]
DLFP8Types = "f4c16678-4a16-415b-82ef-ed337c5d6c7c"
Expand All @@ -34,7 +36,7 @@ MicrofloatsExt = "Microfloats"
[compat]
Adapt = "4.4"
BFloat16s = "0.6"
CUDACore = "6.3"
CUDACore = "6.6"
CUDA_Compiler_jll = "0.6.3"
CompilerCaching = "0.6.1"
DLFP8Types = "0.1.0"
Expand All @@ -49,4 +51,6 @@ PrecompileTools = "1"
Preferences = "1.5"
REPL = "1"
Random = "1.11"
Scratch = "1.2"
TOML = "1"
julia = "1.11"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ julia --project=examples examples/benchmarks.jl # Julia
uv run python examples/benchmarks.py # Python (for comparison)
```

The Julia examples use the tuning records they ship (made on an RTX 5080) where these
apply; pass `--search` to tune the launches that have no applicable record first.

Results comparing cuTile.jl against cuTile Python on an RTX 5080 (`tileiras` 13.2.51, 20
runs, 5 warmup, min time reported):

Expand Down
12 changes: 12 additions & 0 deletions docs/src/lib/host.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,15 @@ CurrentModule = cuTile
Tiled
@__dot__
```


## Autotuning

```@docs
autotune
TuningResult
@tuned
tuned
tuning_store
clear_tuning!
```
181 changes: 173 additions & 8 deletions docs/src/man/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,72 @@ This chapter describes advanced performance tuning for cuTile kernels.

## Choosing tile sizes

cuTile.jl has no autotuner. Tile sizes are ordinary
[`ct.Constant`](execution.md#Compile-time-arguments) arguments, so tuning them is a
loop on the host, with every configuration compiling its own kernel:
Tile sizes are ordinary [`ct.Constant`](execution.md#Compile-time-arguments) arguments
and hints are launch keywords, so the best values for a kernel can be found by trying
them. [`ct.autotune`](@ref cuTile.autotune) does that. It takes a function that
performs the launch for one configuration, and a search space, here every combination
of the values given for each field:

```julia
for (tm, tn, tk) in ((64, 128, 64), (128, 128, 32), (128, 128, 64))
launch() = @cuda backend=cuTile blocks=cld(M, tm) * cld(N, tn) matmul(
space = (tm=(128, 64, 256), tn=(128, 64, 256), tk=(32, 16, 64), occupancy=(2, 1, 4))
result = ct.autotune(space) do (; tm, tn, tk, occupancy)
@cuda backend=cuTile blocks=cld(M, tm) * cld(N, tn) occupancy=occupancy matmul(
A, B, C, ct.Constant(tm), ct.Constant(tn), ct.Constant(tk))
launch() # compile
t = minimum(CUDA.@elapsed(launch()) for _ in 1:20)
println((tm, tn, tk), ": ", round(t * 1000; digits=3), " ms")
end
```

`result.best` is the selected configuration, and displaying `result` ranks all of them:

```
TuningResult: best of 81 configurations is (tm = 64, tn = 128, tk = 32, occupancy = 2)
compiled in 4.0 s, timed in 3.5 s
time (ms) samples configuration
2.422 1.0× 10 tm = 64, tn = 128, tk = 32, occupancy = 2 ← best
2.449 1.01× 10 tm = 128, tn = 64, tk = 32, occupancy = 2
2.457 1.01× 10 tm = 128, tn = 64, tk = 16, occupancy = 4
⋮
81.12 33.5× 1 tm = 256, tn = 256, tk = 16, occupancy = 4
```

This is the kernel of the `Float32` matmul example at 4096³ on an RTX 5080, with the
kernels already in the disk cache; compiling all 81 from scratch took 10–20 s. The
first configuration of the space, the example's hand-picked default, came 10th at
1.03×.

A field can also hold several compiler hints at once, as a `NamedTuple` that is splatted
into `@cuda`. An empty one leaves the kernel's own `@compiler_options` in effect:

```julia
space = (tm=(128, 64), tn=(128, 64),
hints=((;), (occupancy=2,), (occupancy=2, num_worker_warps=8)))
result = ct.autotune(space) do (; tm, tn, hints)
@cuda backend=cuTile blocks=cld(M, tm) * cld(N, tn) hints... matmul(
A, B, C, ct.Constant(tm), ct.Constant(tn), ct.Constant(32))
end
```

The search compiles every configuration first, concurrently, and then times them in
turns, dropping the configurations that fall far behind. Configurations within 2% of
the fastest count as equally fast, and the earliest of them in the space wins, so put
the configuration you would use without tuning first. A configuration that fails to
compile is reported in the table and skipped. The compiled kernels are the ones a
later launch with the same arguments uses, so launching the winner needs no
compilation, and the compiled code is cached on disk (see
[Caching](execution.md#Caching)), so that searching again in a later session mostly
skips `tileiras`.

The search launches every configuration many times on the same arrays. A kernel that
reads what it writes, such as one that accumulates into its output, needs its inputs
restored before each launch: pass a `setup` function, which is called with the
configuration before every launch, or tune on scratch data whose contents do not
matter. `ct.autotune` does not remember its result: use `result.best` to choose the
values in your code, or let the launch site look them up, as described next.

The search times each launch as the host sees it, including the launch overhead of
several microseconds. When that overhead sets the pace, as for a kernel that takes only
a few microseconds, configurations whose kernels differ in speed can measure the same,
and the earliest of them wins.

The following starting points were measured on an RTX 5080 (sm_120) with `tileiras`
13.5, using the examples, without locked clocks.

Expand Down Expand Up @@ -85,6 +137,119 @@ It only covers compiler hints. Tile sizes are chosen on the host, so choosing th
architecture takes a check of `CUDA.capability(CUDA.device())`.


## Tuned launches

[`ct.@tuned`](@ref cuTile.@tuned) makes a launch use the configuration that tuning
found, on whatever GPU, element type and array layout it runs with. The call site lists
the candidates as `autotune` takes them, and `ct.@tuned` calls the function with the
chosen one:

```julia
function matmul!(C, A, B; search::Bool=false)
M, N = size(C)
ct.@tuned(search=search, tm=(128, 64, 256), tn=(128, 64, 256), occupancy=(2, 1, 4)) do (; tm, tn, occupancy)
@cuda backend=cuTile blocks=cld(M, tm) * cld(N, tn) occupancy=occupancy matmul(
A, B, C, ct.Constant(tm), ct.Constant(tn), ct.Constant(32))
end
end
```

Without a tuning record for this launch, the first configuration is used, so list the
values you would pick by hand first. `matmul!(C, A, B; search=true)` instead searches the
space with `ct.autotune` the first time, saves the winner as a record, and launches it;
every later call, in this session or another, uses the record, whether or not it passes
`search=true`. A search takes seconds (the 81 configurations of the `ct.autotune`
example above took about 8 s with their kernels in the disk cache, and 10–20 s without),
so searching is off by default and is for the code that calls `matmul!` to ask for. A
library can offer one switch for all its tuned launches by defaulting the keyword to a
setting of its own, as cuDNN.jl's `autotune` keyword does.

The space can also be given positionally, as a collection of `NamedTuple`s, which is how
to leave out combinations: `ct.@tuned(((; tm, tn) for tm in (128, 64), tn in (128, 64) if
tm >= tn); search) do (; tm, tn) … end`.

### Which record applies

Records are found by what the launch compiles to, not by a name. Before choosing,
`ct.@tuned` calls the function once with the first configuration while launches only
compile, as `autotune` does, and looks up the record for the code of the kernels this
launches, the search space and the GPU's compute capability. A record therefore applies
again after a restart or in another installation, and stops applying when the code that
the first configuration compiles to changes: when the kernel or anything it calls is
edited, or when it is launched on another element type or array layout (a view instead
of a whole array, for instance). Another search makes a new record; nothing needs to be
declared or invalidated by hand. Changes that only affect other configurations, such as
code reached only with larger tiles, are not noticed.

Values that change which configuration is fastest without changing the kernel's code
are not seen, the size of the problem in particular. Pass them as `key`, rounded so that
similar values share a record: `ct.@tuned(key=(nextpow(2, M), nextpow(2, N)), …)`.

The choice is remembered for the rest of the session per call site: the type of the
function passed to `ct.@tuned`, which includes the types of the variables it uses,
`key`, the space and the GPU. Calls that differ in a value the types do not show, such
as a head dimension passed as a `ct.Constant`, therefore launch the configuration chosen
for the first of them, until a method is defined anywhere, which makes every call site
look again. Put such values in `key` as well.

### What the launch function may do

The function passed to `ct.@tuned` is called more than once: to identify the launch, and
many times during a search. It must only compute on the host and launch cuTile kernels
on the task's stream, and which kernels it launches must not depend on the results of
earlier launches in the same call. Other GPU work in it, such as a `copyto!` or a
CUDA.jl kernel, runs on every call, also while identifying the launch. A function that
launches no cuTile kernel is rejected.

A search launches the kernels many times on the arrays the call was given. For a kernel
that reads what it writes, pass `setup`, which the call that searches calls with the
configuration before every launch of the search and once more before the launch that
counts, to restore the inputs (see `ct.autotune`). Searching while capturing a CUDA graph throws an error. When
several tasks reach a launch that has no record at the same time, one of them searches
and the others wait for it, then launch with the result.

### Where records are kept

Each record is a small TOML file holding the chosen configuration, the times of all
configurations, and the GPU, cuTile and `tileiras` versions it was made with. Calls in a
package keep them in a scratch space of that package, which
[`ct.tuning_store`](@ref cuTile.tuning_store)`(MyLib)` returns and which is removed
with the package; calls outside a package, as in a script or the REPL, use the scratch
space `Scratch.@get_scratch!` uses there (the active project's if it is a package, and
otherwise a global one). Pass `store=dir` to keep them in a directory of your choice. When the
store cannot be written to, the record is kept in memory for the session and a warning
is shown. [`ct.clear_tuning!`](@ref cuTile.clear_tuning!)`(MyLib)` deletes the records of
a package and makes this session forget its choices, so that the next call with
`search=true` searches again, unless a shipped record applies. Deleting one of the files
does the same for one record from the next session, or the next method definition, on.

A package can ship records for the GPUs it was tuned on: tune, copy the files from its
store into a directory of the repository, and pass that directory as `shipped`:

```julia
ct.@tuned(shipped=joinpath(pkgdir(MyLib), "tuning"), search=search, tm=…) do cfg … end
```

A shipped record applies wherever cuTile generates the same code for the launch, in
practice with the same cuTile minor version, on a GPU of the same compute capability.
Records made on a GPU with the same name come first, then those from other GPUs; within
each, the store comes before the shipped records, so that a user's own search takes
precedence. As the search space is part of what identifies a record, changing it, even
only the order of its values, also takes new records.

The examples in the repository work this way. Their launches are tuned, with records for
an RTX 5080 in `examples/tuning`, and their `run` functions take `search=true`, which
the benchmark runner passes when asked:

```bash
julia --project=examples examples/benchmarks.jl --search matmul fmha
```

This searches the launches that have no applicable record, as on a GPU of another
compute capability, keeps the new records in the global scratch space (see
`ct.tuning_store(Main)`), and reports the times with them.


## Kernel configuration

`ct.@compiler_options` sets optimization hints inside a kernel function body:
Expand Down
5 changes: 3 additions & 2 deletions docs/src/tutorials/matmul.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,9 @@ computes:
occupancy, which matter a lot for GEMM. See
[Performance](../man/performance.md).

`examples/matmul.jl` in the repository is this kernel plus both of those, and
`examples/batchmatmul.jl` extends it to batched operands using trailing batch
`examples/matmul.jl` in the repository is this kernel plus both of those, with
its tile sizes and hints chosen by [`ct.@tuned`](../man/performance.md#Tuned-launches),
and `examples/batchmatmul.jl` extends it to batched operands using trailing batch
dimensions on `muladd`. For block-scaled FP8 and FP4 matmuls, see
[`ct.muladd_scaled`](../lib/operations.md#Matrix-multiplication) and
`examples/blockscaled_matmul.jl`.
2 changes: 1 addition & 1 deletion examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cuTile = "0dea8319-8c4a-4662-a73d-20234d115b9a"
cuTile = {path = ".."}

[compat]
CUDA = "6.5"
CUDA = "6.6"
FFTW = "1.10.0"
Microfloats = "0.2.6"
NVTX = "1.0.3"
Loading