Skip to content

Add a tuning script - #1211

Open
christiangnrd wants to merge 21 commits into
mainfrom
tuning
Open

christiangnrd wants to merge 21 commits into
mainfrom
tuning

Conversation

@christiangnrd

@christiangnrd christiangnrd commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Do not merge yet. testing on apple-m1 only to not hog the base julia runners and so the suite duration comes from the slowest device

Tests a whole bunch of combinations.

Took 1h40 on M1, 1.5h on M2, but it monitors thermal state and pauses when it gets “serious” so could take longer on fanless devices.

Partial runs are still valuable, and the tar archive gets updated after every suite, and when ctrl+c. Also supports resuming. See the in-folder README for the most up to date info

maleadt and others added 14 commits December 3, 2026 11:42
`tiles.jl` times the tiles that matmul2d accepts over about 100 product
shapes, and `auto.jl` the native kernels against MPSGraph end to end, which
is what `:auto` decides between; `run.sh` runs both and packs the results.

These are the files of
https://gist.github.com/maleadt/266deb5322ce9317ced0ed9c6033d919, unchanged.
`run.sh` installed Metal.jl from `tb/auto-native`, which no longer exists.
`run.jl` defines `tune()`, and runs it when called as a script: it runs
the suites against the checkout it is in, with the Julia binary it was
called with, still each in a process of its own, so that a suite that
fails or aborts only loses its own results.

It also runs the relaxed suites of `auto.jl` on the GPUs that relax (M5
and later), takes the suites, element types and rounds to run, and can
resume a run that was interrupted. Next to the timings it records the
GPU, the commit of Metal.jl that the suites ran, marked when tracked files
differ from it, the version of Julia, when the run and each suite started,
and how long each suite took: the timings of a rule's selection can't be
read without the commit that the rule is from.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The rows of `tiles.jl` end with the tile and the number of slices that the
call ran with, whether they were given or selected, and the `# shape` line
has what the rules select on their own: with an explicit tile the slices
still follow the rule, and nothing said how many there were. The methods
are now sets of keywords of `gemm_tensor!`, from which both the call and
its plan are made.

The rows of `auto.jl` end with the algorithm that `:auto` selects for the
product.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`tiles.jl splits` times the number of slices that `gemm_tensor_splits`
picks against explicit ones, with the tile left to the rule: 1 to 32,
including counts that aren't powers of two, as the rule often selects one
(9 slices beat both 8 and 16 for 128² products on an M2 Max), and 64 for
the longest contractions, past the 32 that the rule stops at.

Its 64 shapes are products with few threadgroups and contractions of 2048
to 32768, ones around the 512² to 1024² band whose long contractions are
split whatever their threadgroups, and four with a transposed operand.
They include the places where the rule was off on that machine: 333×222,
which it leaves whole where 4 to 16 slices are 1.3-1.9× faster; 1536² and
2048×1024 with a contraction of 16384, just above the band (1.2-1.3×);
and 1024²×8192 inside it, which is faster whole (1.1×).

The tile suites don't cover this: the rule splits at most 2 of their 105
shapes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`gemv.jl n` and `gemv.jl t` time `gemv_n_kernel!` and `gemv_t_kernel!` with
the parameters that `gemv_config` selects and with explicit ones, next to
the tensor and simdgroup GEMM kernels, which ran these products before, and
to MPSGraph. The first round checks every result against a CPU product, as
most of the explicit parameters are never selected otherwise.

The shapes include those where the kernels are slower than what they
replaced on an M2 Max, with any parameters: A·x with few rows, and
transpose(A)·x with columns shorter than 64. Timing the GEMM kernels next
to them is what a rule to fall back needs.

For those shapes the tensor kernel is also timed with explicit tiles and
numbers of slices. Its rules for them weren't fitted to single columns, so
the fallback should be set against the best that it does. It makes a
difference for A·x: a 1×2^24 `Float32` product takes 114 ms with the
matrix-vector kernel, 18 ms with the tensor kernel in the 32 slices that
its rule stops at, and 1.4 ms in 512. With the best tile and slices the
tensor kernel is as fast as the matrix-vector one up to about 2048 rows of
a matrix of 2^24 elements, not 1536. For the transpose the tile matters
little.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Since #1196 the native path runs products with a single column with the
matrix-vector kernels, so the tile that matmul2d would get for them no
longer matters for the element types that these suites time; `gemv.jl`
covers them. That is 8 of the 105 shapes.

`tiles.jl ab` timed the tile rule from before #1191, written out in the
script, against the current selection. That checked #1191; the earlier
rule isn't a candidate any more, and on the larger M1 and M2 chips it is
what the current rule selects for all but the smallest products.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The rules know two kinds of inputs, those of at most 2 bytes and others,
and were fitted with `Float16` and `Float32`. `tiles.jl types` times the
tiles over 12 shapes for `BFloat16`, `Int8`, `UInt8`, `Float16` with
`Int8`, and `Float32` with `Float16` and with `Int8`, next to those two,
to see whose ranking of the tiles a type follows, and so whether it needs
rules of its own. Types that the system's matmul2d doesn't take are
skipped. Integer inputs also get two products with a single column, which
the matrix-vector kernels don't run for them.

For that, a measurement takes the element types of A, B and C, with whole
values for the integer ones, and `tiles.jl` loads BFloat16s, so `T` can be
`BFloat16` in its other suites too.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
What the suites time and which rule each is for, how to run them, and
what a run records about the machine.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A machine that is throttled for heat, or in Low Power Mode, gives timings
that say nothing about its rules, and the CSVs had no way to tell. As the
app that replays the suites on iPhones does, every row now ends with the
thermal state that it was timed in, as `fair→serious` when it changed
meanwhile, and with whether Low Power Mode was on, and nothing is timed
while the state is serious or critical: a suite waits for fair or nominal
and says in a comment how long that took.

The clocks of the GPU are down after such a wait, so it is then kept busy
for 0.3 s with what is timed next, as before a round.

`run.jl` has the conditions at the start in `info.txt` and those of each
suite in `run.log`, and says when a suite left nominal, ran in Low Power
Mode or had to wait.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The results are meant to be sent in from other people's machines, so what
they say about the machine comes before how to run the suites, with the
one way that a path with the user's name gets into them: the stack trace
of a suite that failed.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The archive was only made at the end of a run, so one that was stopped,
or that the machine didn't get through, left its results to be packed by
hand. It is now brought up to date after every suite, written under
another name first so that a run that is killed meanwhile keeps the
previous one.

Ctrl-C ended Julia, and with it the run, before `tune` could do anything.
It now reaches `tune` as an exception, which stops the suite in progress,
logs it as `interrupted`, packs what it had timed with the rest and ends
the run. `resume` runs that suite again.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Their paths are relative to the root of the checkout, which the README
left to be guessed. `run.jl` finds the checkout from its own location, so
it is the one that also works from elsewhere.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The splits suite timed the tensor kernel only, and the suites of `auto.jl`
time it only with the slices that the rule selects. So the results
couldn't say whether another number of slices makes up for what the
kernel is behind MPSGraph. The suite now times `MPSGraphs.matmul!` on the
same operands as well.

It also gets the products where the kernel is furthest behind MPSGraph in
`auto.jl`, which it didn't have: the long contractions of the `longk`
suite and some products with few columns of `fewcols`, 18 shapes in all.
64 slices are tried from K = 20000 instead of 32768, which only changes
what is timed for the new shapes.

The suite takes about 40% longer: 14 instead of 10 minutes on an M2 Max
for both element types.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The splits suite times the number of slices with the tile that the rule
selects only, and the tile suites time each tile with the slices that the
rule gives it, and hardly any product with a long contraction. So nothing
said whether another tile makes up for what the tensor kernel stays behind
MPSGraph with the fastest number of slices.

`tiles-joint` times six tiles, each with the rule's slices and with 4, 8,
16 and 32 of them, next to the rule's plan and MPSGraph. It does so for
the 21 products of the splits suite where the kernel stays over 10% behind
MPSGraph on an M2 Max with the fastest slices, for either element type.
The tiles are those that the rule selects for these products on any GPU.
A method that takes over 3 times as long as the fastest in the first round
is left out of the others, as in the tile suites.

The suite takes 8 minutes on an M2 Max for both element types.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
christiangnrd and others added 3 commits December 3, 2026 15:53
`tiles-types` is the longest suite, 24 minutes on an M1 and about a fifth
of a run on every Mac. What it checks, that an element type belongs with
`Float32` or with `Float16` by the size of its inputs, has held up on the
four Macs that ran it. The results are meant to come from other people's
machines, who shouldn't have to wait for it.

A run now leaves the suite out unless it is named, and so does the `tiles`
group. `default` stands for the suites that run by default, so that it
can run along with them and end up in the same archive:
`julia perf/tuning/run.jl default tiles-types`.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
It followed `Int8` on every Mac that ran the suite: within half a point of
it in how far the rule and either class of tiles are off, and as fast.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The suite timed a fixed list: the 21 products of the splits suite where
the tensor kernel stayed over 10% behind MPSGraph on an M2 Max with the
fastest number of slices. An M1 has 32 such products for `Float32` and 22
for `Float16`, and only 13 and 12 of them were on the list. Every GPU
would have needed its own additions, and another run.

`tiles-joint` now reads what the splits suite of the same run timed for
its element type, and times the products that this left over 10% behind
MPSGraph: the furthest behind first, and 30 at most (`JOINT_MOST`).
`run.jl` tells the suites where the results are. An element type now only
times its own products, rather than those of either.

Nothing was timed with this yet. Against the splits results of the M1
and of the M2 Max, it selects 30 and 22 products on the former and 18 and
12 on the latter.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
`mpsgraph_matmul_faster` was fitted from the auto suites alone, which time
the native kernel with the plan that the rules gave it at the time of the
run. A change to the tile or slice rules made their results stale for the
products that it gave another plan, and those suites had to run again.

The sweep, trans and valid suites of `tiles.jl` now time MPSGraph on the
same operands as the tiles, as the splits, joint and matrix-vector suites
did already. `:auto` can then be scored for their products under whatever
rules, from the time of the tile that they select. Tiles are pruned
against the fastest tile, so that MPSGraph neither prunes them nor is
pruned.

`auto.jl` leaves out the products that those suites cover: 58 of its 180
cases per element type, where nothing but the shape is specific to the
call. Products of fewer than 2^28 multiplications stay, since those suites
call the kernels rather than `mul!`, and what it adds to a call shows for
them: in earlier results, a kernel alone took half the time of `mul!` for
512x512x512 on an M2 Max, and a third of it for 256x256x256. Above that,
the two were within 5% of each other on average on an M1.

Not timed as a whole yet. The cases of `auto.jl` were listed without
timing them (180 before, 122 now, none added), and one product was run
through the sweep and types suites to see that the former writes rows for
MPSGraph and the latter doesn't.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
christiangnrd and others added 3 commits December 3, 2026 16:46
A run takes an hour or two that nobody needs to sit through, and nothing
kept the Mac from going to sleep meanwhile, which pauses the suite in
progress and makes its duration in the log meaningless.

`tune` now starts `caffeinate -i`, which holds off idle sleep, and ends it
when the run does: finished, failed or interrupted. Should the runner be
gone before that, `caffeinate` ends by itself (`-w`). It can't keep a
laptop awake with its lid closed, which the README and the run say.

`info.txt` also has whether the Mac was on power or on its battery when
the run started, and whether it was kept awake. The README asks for power,
and until now only Low Power Mode was recorded.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Removes every step of the Buildkite pipeline and runs the tuning suites on
an M1 runner instead, as `julia perf/tuning/run.jl` runs them by default,
with the archive of their results as an artifact.

Also comments out the push and pull request triggers of the CI, Load and
Documentation workflows, so that GitHub Actions doesn't run them for this
branch meanwhile. They can still be started by hand.

To be dropped before this branch is merged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

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.

2 participants