ML compiler eng, your job is NP-hard
While layout selection in ML compilers is theoretically NP-hard, practical compiler design often relies on heuristics that perform surprisingly well.
Deep dive
- Layout selection involves choosing memory layouts (tensors) that minimize data movement costs between operations.
- Amazon researchers proved layout selection is NP-hard, yet simple rule-based heuristics often out-perform theoretically 'optimal' solvers on current hardware.
- Triton uses a 'RemoveLayoutConversions' pass that applies simplified cost rules to prune unnecessary memory shuffles.
- Performance bottlenecks often shift from the layout search itself to how backend compilers (like NVIDIA's ptxas) handle register pressure and instruction ordering.
- Rematerialization is often more effective than attempting to optimize layout conversions, as it may avoid memory spill costs entirely.
Decoder
- Layout Selection: The task of deciding how to arrange data in memory (e.g., NCHW vs NHWC) to optimize performance for specific hardware kernels.
- ptxas: The NVIDIA PTX assembler that converts high-level intermediate code into machine code for GPUs, responsible for low-level register allocation.
- Rematerialization: A compiler optimization where a value is recomputed instead of being loaded from memory, trading computation time for reduced memory traffic.
Original article
ML compiler eng, your job is NP-hard
I mean, sorta. From the Tensor Seeks Layout paper 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 of work 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 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, 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 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.
The finding that layout search is computationally hard in Amazon’s paper is valid, and good to share with your executives. 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. 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.
- And the pass is commented“TODO: add a proper heuristic”, so it’s not like this was a surprise
- in this case it moved one
local_load6 ops earlier, which caused ptxas to drop from 255 registers to 40. I don’t know why! - 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.
- 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
Fresh dev news, AI-digested daily. Never miss a trend at devoured.fyi