Writing

Fuse it only if it pays

Weft used to fuse attention wherever it saw the shape. The engine that replaced it prices the decision on the machine in front of it, and the first thing it did was undo a fusion.

1 Oct 2026 · Updated 2 Oct 2026 · 7 min · Ernest

Compilers & SIMD, Negative results

Until October, fusion in Weft was pattern matching. A pass walked the plan, spotted the six kernels of an attention chain, and swapped them for one fused body I'd written for exactly that shape. Nothing checked whether fusing actually paid.

On the machine where I wrote it, it paid. The fused body beat ggml's attention by about 1.3× per head on a Zen+ box, and turning it on took 29% off the whole SAM 3 image model on a Threadripper.

Then I ran the same body on a rented Broadwell Xeon. ggml took 113 ms per head. Mine took 175. The thing the matcher had been inserting on sight was 1.54× slower than the code it was supposed to beat, and the matcher had no way to know.

Fixing the body

The body was fixable. I rewrote it with the queries in the vector lanes instead of the keys, which kept both contractions in registers and cut the number of times K and V came back from L3 from 1,296 per head to eleven. Same operations in the same order, so the output stayed bit-identical. 64 ms per head, and on that box the whole model went from 1.45× ahead of ggml to 2.07×. A week later the segmentation-head convolutions got the same treatment.

MachineSettingggml ÷ Weft
x64, host not noted (the earlier article)not noted0.27 (3.75× behind)
Xeon E5-2680 v4, BroadwellInference2.07
Ryzen 9 3900X, Zen 2Inference2.29 to 2.31
Ryzen 9 3900X, Zen 2Strict1.04 to 1.05
SAM 3 image model against sam3.cpp on ggml, one thread each, same core of the same rented box. ggml's time divided by Weft's, so above 1 means Weft is faster.

Under Strict it's level. That's fair: Strict forbids reordering floating-point sums, the online softmax depends on exactly that, so the biggest fusion in the model isn't available.

But I'd fixed one body for one machine, and the problem was never really the body. Per head the new attention was 1.76× faster than ggml. In the whole model it bought 1.43×. A number in isolation and a number in context are different numbers, and the context changes with the machine.

Pricing instead of matching

So the matchers went. The composition engine that merged on 1 October has no patterns. It grows a region backwards from any kernel through the producers of its inputs, for as long as nothing outside needs the intermediate result. Attention falls out of that, and so do a few fusions nobody ever wrote a matcher for.

The one clever part is a single algebraic rewrite: fold a sum against a running maximum and correct it afterwards. That's what makes attention one pass instead of three, and the engine derives it from the kernel bodies rather than knowing what a softmax is.

Then it prices. A cost model in cycles goes first. Where the model's margin is thin, there's a short timed race inside the compile budget, and the decision gets written down so the next compile doesn't race again.

The first thing it did

The first clean measurement was SAM 3 on an Apple M4, four processes per arm, order reversed every round. The matchers' plan, with fused attention, took about 2,510 ms. The engine's plan left attention unfused and took about 1,720. Every process of one arm beat every process of the other. On that machine, the fusion I'd been applying on sight was a 1.46× loss.

In isolation, on the same M4, the fused attention is several times faster than the six kernels it replaces. In the model it loses. I don't know why yet.

The engine is still rough. Pricing is slow, so inside the compile budget it gets through a handful of SAM 3's 644 candidates and leaves the rest unfused. That happens to be right on the M4. One early run on x64 decided nothing at all, rendered the same plan on both sides, and reported the result as a ratio of 0.9999 with ok printed next to it.

Next is making the pricer fast enough to reach attention inside the budget. I haven't measured the engine on x64 yet.