# ML compiler eng, your job is NP-hard

> Source: <https://ianbarber.blog/2026/11/24/ml-compiler-eng-your-job-is-np-hard/>
> Published: 2026-11-25 01:12:17+00:00

**Recommendation:** Before rewriting layout-conversion heuristics, check ptxas register spills and instruction ordering: one matmul ran 15–26x faster from ordering alone, while a corrected cost model changed 9 of 21 kernels not at all.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

I mean, sorta. From the [Tensor Seeks Layout paper](https://arxiv.org/abs/2608.21555v1) from Amazon:

Layout Selection is NP-hard even when each operator has only a constant number of inputs and each tensor admits only a small number of layouts, and hardness persists in restricted settings that resemble common matmul-heavy graphs.

The paper is very focused on formal analysis of the layout problem, for Trainium workloads. They build a layout selection exact solver, and on ResNet-50 they find the proven optimum using their cost model takes 63.9M cycles. In contrast, the awkward heuristic rules take… 20.9M cycles. The authors attribute this to a wonky cost model, which we will come back to.

There has been a [lot](https://arxiv.org/abs/2603.02298) of [work](https://arxiv.org/pdf/2601.05972) in recent months on what layouts actually *are*, but this paper is about where and when you do conversions between them.

To jump down a level of granularity, in the Triton compiler this decision is mainly handled by the *RemoveLayoutConversions* pass: during compilation, Triton throws in layout conversions every time one might be needed, and this pass prunes them. It has the very sophisticated rule that conversions either cost `0` or `32xbytes`. “0” means the conversion is a register permute, where everything happens locally. The higher value is for shuffles and shared memory round trips.

This made me wonder how wrong the rule actually was, versus the more structured approach in Amazon’s paper. I [ran some experiments](https://github.com/ianbarber/experiments/tree/main/2026-11-21-triton-layout-cost-weights) and found, yeah, the heuristic is pretty wrong. Relative to an elementwise add, the heuristic overestimates by about 4x for fp32, 19x for fp16, and sometimes even more for fp8. If you update to use the proper cost model though, not a lot changes. No tutorial kernels shift, and in general the performance changes are a bit of a wash: in testing 8 were slower, 4 were faster, 9 didn’t change at all.

The main reason is rematerialization: instead of converting the layout, walk back up the chain and recompute or load in the format you actually need. Conversions are mostly about cases where you can only emit one way, and then consume a different way. In those cases, the layout picks are arbitrary<sup>[1](#fn-07c805a3-1aee-447f-8a51-8f67d51ad5bc)</sup>, but they are mostly fine: the majority of kernels didn’t require a choice, and where they did, the default pick seemed to be the right one the vast majority of times.

We talked [earlier](https://ianbarber.blog/2026/11/09/make-this-forward-pass-as-fast-as-possible/) about OpenAI’s effort to remove heuristics from the compiler, but this seems like an area where a heuristic is doing a reasonable job. If you dig into the cases where there *is* a performance mismatch though, you will find that the problem is also a compiler, but in this case a different one. 

The pass exists to cut down layout conversions, which is generally good! But the output is then fed into another compiler, `ptxas`. That can be sensitive: in one case we found a matmul that got 15–26x faster, purely because of the instruction ordering. `ptxas` decides register allocation, and if it decides there aren’t enough it will spill to memory, significantly impacting performance<sup>[2](#fn-c832799c-4e57-46d2-a0a9-a5a59bf0379a)</sup>.

The finding that layout search is computationally hard in Amazon’s paper is valid, and good to share with your executives<sup>[3](#fn-78e329a4-4e30-449e-8854-3e240fe7446a)</sup> . But, in reality, you don’t have to check all that many options: there are generally just a few places to put a conversion in a specific kernel<sup>[4](#fn-1f1cf7c2-66ab-4df5-acd1-ed9d84040771)</sup>. Picking an option requires relying a potentially inaccurate model of how expensive those ops are given the subsequent (closed-source!) software stack *and* the hardware itself. And that seems to be the really tricky part.

1. And the pass is commented“TODO: add a proper heuristic”, so it’s not like this was a surprise [↩︎](#fn-07c805a3-1aee-447f-8a51-8f67d51ad5bc-link)
2. in this case it moved one `local_load` 6 ops earlier, which caused ptxas to drop from 255 registers to 40. I don’t know why![↩︎](#fn-c832799c-4e57-46d2-a0a9-a5a59bf0379a-link)
3. We have to take these wins in the brief time before someone puts out 150M lines of Lean to prove that NP isn’t that hard at all. [↩︎](#fn-78e329a4-4e30-449e-8854-3e240fe7446a-link)
4. To be fair, the Amazon paper is doing the same thing for entire graphs of 10k+ operators, but it seems like it is somewhat analogous [↩︎](#fn-1f1cf7c2-66ab-4df5-acd1-ed9d84040771-link)
