{"slug": "ml-compiler-eng-your-job-is-np-hard", "title": "ML compiler eng, your job is NP-hard", "summary": "A paper from Amazon, \"Tensor Seeks Layout,\" proves that layout selection for ML compilers is NP-hard even when each operator has only a constant number of inputs and each tensor admits only a small number of layouts. Independent experiments by Ian Barber on Triton's RemoveLayoutConversions pass found the compiler's heuristic overestimates conversion cost by about 4x for fp32 and 19x for fp16 relative to an elementwise add, yet swapping in a proper cost model changed little: of 21 kernels tested, 8 were slower, 4 faster and 9 unchanged. Barber attributes the remaining performance gaps to ptxas register allocation, citing one matmul that ran 15–26x faster purely from instruction ordering.", "body_md": "I mean, sorta. From the [Tensor Seeks Layout paper](https://arxiv.org/abs/2608.21555v1) from Amazon:\n\nLayout 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.\n\nThe 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.\n\nThere 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.\n\nTo 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.\n\nThis 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.\n\nThe 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.\n\nWe 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. \n\nThe 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>.\n\nThe 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.\n\n1. 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)\n2. 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)\n3. 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)\n4. 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)", "url": "https://wpnews.pro/news/ml-compiler-eng-your-job-is-np-hard", "canonical_source": "https://ianbarber.blog/2026/11/24/ml-compiler-eng-your-job-is-np-hard/", "published_at": "2026-11-25 01:12:17+00:00", "updated_at": "2026-11-25 01:14:04.427243+00:00", "lang": "en", "topics": ["machine-learning", "ai-research", "ai-infrastructure", "developer-tools"], "entities": ["Amazon", "Triton", "Ian Barber", "ptxas", "ResNet-50", "Trainium", "OpenAI"], "also_reported_by": [], "takeaway": "Triton's layout-conversion heuristic overestimates cost by roughly 4x for fp32 and 19x for fp16 versus an elementwise add, but replacing it with a proper cost model left 9 of 21 kernels unchanged, 8 slower and 4 faster, so the bigger lever is ptxas register allocation and instruction ordering.", "recommendation": {"advice": "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.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/ml-compiler-eng-your-job-is-np-hard", "markdown": "https://wpnews.pro/news/ml-compiler-eng-your-job-is-np-hard.md", "text": "https://wpnews.pro/news/ml-compiler-eng-your-job-is-np-hard.txt", "jsonld": "https://wpnews.pro/news/ml-compiler-eng-your-job-is-np-hard.jsonld"}}