← all posts
September 20, 2026·Ronald Adonyo· 9 min read

Running Apple's compiled GPU kernels on the open driver

We lifted Apple Metal's compiled q4_0 matvec machine code out of a macOS capture and executed it byte-intact inside Mesa's Honeykrisp Vulkan driver on Asahi Linux — same M2 Ultra, same model. It passes an exact CPU reference and runs the mission decode matvec 1.8x faster than the kernel the open driver compiles, splitting the decode gap into a measured kernel-code half and a driver-execution half.

70.5 µsApple binary on the open driver
126.5 µsHoneykrisp's own kernel
48 µsApple binary on Metal
2212load faults consumed
0.0000658max error
gpureverse-engineeringasahi-linuxvulkanmetalbenchmarking
Two wooden frames in a swept yard before a small house, the left one empty with a pale outline and the right one holding a carved block fitted exactly; a chimpanzee presses the block home, a baboon sits on the low wall and a crested crane at the right looks at the block.

An ⁠Apple ‌M2 ‌Ultra ‌running ‌llama.cpp ​through ‌the ​open-source ​stack ‌decoded Qwen3-4B ‌at ‌2.5× ​slower ‌than ‌the ‌same ‌chip ‌on ‌macOS. ‌The ​gap ‌had ‌already ​been decomposed ‌for ​months: ​the ​biggest ‌single ​contributor ‌was ​one ‌shader ​— ​the q4_0 ​matrix-vector ‌kernel ‌— ​running ​2.61× ‌slower ⁠than ⁠the ‌equivalent ‌Metal kernel. ‌The ‌question ​that ‌mattered: ​is ​Apple's ‌kernel ‌code ‌that ​much better, ‌or ‌is ‌their ‌driver ‌executing ‌better? ‌Those ​have ‌opposite ‌fixes.

The mechanism: hash-gated binary substitution

We ​captured ‌Apple's ​compiled ​kernels ​by ‌hooking ​the ‌Metal ​device ‌at ​model load ​on ​macOS ‌(25 ‌pipeline ​archives, ​16 ‌distinct ⁠binaries). ⁠On ‌Linux, ‌a ‌Mesa patch ‌computes ​a ‌BLAKE3 ​hash ​of ‌every ‌compiled ‌compute ​pipeline's ‌serialized NIR; ‌when ‌it ‌matches ‌a ‌recorded ‌key ​from ‌the ‌capture, ​the ‌driver ​substitutes Apple's ​machine ​code ‌for ​its ‌own ​— ‌same ​pipeline, ​same ​descriptor ‌bindings, same ‌push ​constants. ​A ‌40-word ⁠uniform ⁠block ‌built ‌per ‌dispatch ‌carries everything ​the ‌Apple ​ABI ​expects: ‌the ‌three ‌buffer ​addresses ‌as ‌64-bit ‌pairs, a ‌block ‌count ‌for ‌the ​K-loop, ‌batch ‌strides, ​dequant ‌masks, ​fp16 ​scales.

text
macOS capture (MTLBinaryArchive) ──▶ sidecar (preamble+main+ABI)
llama.cpp q4_0 decode ──▶ NIR hash (blake3) ──▶ key match?
   ├─ yes ─▶ driver substitutes Apple binary ─▶ GPU runs Apple machine code
   └─ no  ─▶ driver runs its own kernel

What the binary taught us before it stored a single byte

The ​first ‌runs ​produced ‌beautifully ​deterministic ‌zeros. ​Every ​value ​in ‌the destination ‌stayed ​exactly ​0.0 ‌across ⁠runs ⁠— ‌the ‌kernel ‌executed, ‌loaded ​real activations, ‌ran ​its ​full ‌K-loop, ‌and ‌stored ​nothing. ‌Three ‌wrong ‌theories later, ‌the ‌answer ‌was ‌one ​uniform: ‌u11, ‌the ​output-row ‌limit. ​Apple's stores ​are ​predicated ‌row < u11 && lane == 0, ​and ‌our ​uniform ‌builder ​had that ​slot ​at ‌zero ‌— ​every ​store ‌in ⁠every ⁠workgroup, ‌predicated ‌off.

The ‌second ‌lesson ​was ‌about ​our ​own ‌verification: ‌our ‌first ​reference implementation ‌decoded ‌q4_0 ‌nibbles ‌interleaved ‌(low, ‌high, ‌low, ​high) ‌where ggml ‌packs ​them ‌half-split ​— ​the ​first ‌16 ​values ‌from ​the ‌low ​nibbles, ​the second ​16 ‌from ‌the ​high. ​With ‌the ⁠wrong ⁠reference, ‌both ‌the ‌CPU ‌and ​GPU ‌paths "failed" ​identically, ​which ‌looked ‌like ‌a ​kernel ‌bug ‌and ‌was ‌a ‌test ‌bug.

The measurement

With ‌the ​store ‌predicate ‌fixed ​and ‌the ​uniform ​map ​decoded ‌from ​the ‌binary's own ​dataflow ‌(three ​buffer ​addresses ​as ‌64-bit ‌pairs, ​the ​K-loop ‌block ⁠count, batch ⁠strides, ‌dequant ‌masks, ‌fp16 ‌scales), ​the ‌injected ​kernel ​stores ‌correct results ‌at ‌the ​mission ‌decode ‌shape: ‌max ‌error ‌0.0000658 ‌against ‌an ​exact ‌CPU reference, ‌and ​full-model ‌greedy ​decode ​byte-identical ​to ‌the ​pinned ‌hash.

kernelq4_0 matvec, 4096×14336
Honeykrisp's own126.5 µs
Apple binary on the open driver70.5 µs (1.8×)
Apple binary on Metal48.0 µs (shared-weight 16-slice protocol — see note)

Two ​independent ‌deficits, ​previously ​conflated, ​separated: ‌Apple's ‌kernel code ​is ​genuinely ‌better ⁠than ⁠what ‌the ‌open ‌stack ‌compiles ​(1.8× ‌on ​the ​same driver), ‌and ‌Apple's ‌driver ​executes ‌the ‌same ‌machine ‌code ‌1.47× ‌faster ‌(22.5 µs ​per ‌dispatch ‌of ​launch, ‌scheduling ​and ​clock-policy ​difference ‌that ​no shader ‌improvement ​can ‌touch). ​Fixing ​the ​first ‌without ‌the ​second ​caps ‌short of ⁠parity; ⁠the ‌decomposition ‌says ‌which ‌community ​effort ‌addresses ​which ​half.

Corrected ‌2026-10-19: ‌the ‌historical ​Metal ‌48 ‌µs ‌row ‌is ‌a ‌shared-weight 16-slice ‌protocol, ​not ‌a ‌like-for-like ​decode ‌figure ​— ​the ​Metal ‌capture divides ​one ‌shared-weight ​MUL_MAT ‌into ​16 ​activation ​slices, ‌so ‌it ​is ​not ‌a per-decode-dispatch ⁠number ⁠and ‌must ‌not ‌be ‌read ​alongside ‌the ​open-driver ​rows (campaign ‌contract ‌O6). ‌The ​apples-to-apples ‌comparison ‌in ‌this ‌table ‌is ‌the open-driver ‌pair: ​126.5 ‌µs ‌(Honeykrisp's ​own) ‌versus ​70.5 ​µs ​(Apple ‌binary ​on the ‌same ​driver).

Why it mattered

The ‌injected ​pair ​is ​a ‌disassemblable, ‌diffable ​specification ​of ‌what ⁠Apple's compiler ⁠emits ‌versus ‌what ‌the ‌open ​compiler ‌emits ​— ​the ‌itemized ‌list ‌the open ​backend ‌needs, ‌generated ‌by ‌the ‌vendor's ‌own ‌compiler ​rather ‌than inferred. ‌The ​same ‌capture ​holds ​Apple's ​compiled ‌versions ​of ‌every ​kernel ‌in the ​decode ​graph, ​with ‌injection ‌sidecars ​staged. ​The ‌companion-binary interface ⁠this ⁠experiment ‌created ‌is ‌the ‌same ​one ‌that ​later ​carried ‌the payload ‌revision ‌worth ​+13% ‌model-level ‌decode ‌— ‌the ‌measurement infrastructure ‌built ‌to ​answer ‌one ‌question ​turned ‌into ​the ​optimization vehicle ​for ‌the ​whole ‌campaign.

boracode.ai · Ronald Adonyo · apple-gpu-kernels-on-mesa-vulkan
content hash f4efed1519d3
A wide horizon at dusk: layered hills in muted plum and ochre above a lake that holds the last light, a family of crested cranes at the water's edge, an acacia in silhouette and a single bright star above the ridge.