布局选择被证明为 NP-hard 难题

原标题:ML compiler eng, your job is NP-hard
58AI 研判分

亚马逊论文《Tensor Seeks Layout》形式化证明了张量布局选择问题即使在各算子输入数恒定、张量候选布局极少的受限场景下仍为 NP-hard,且该结论在类似常见矩阵乘密集计算图的设定下依然成立。作者为 Trainium 负载构建了精确求解器,在 ResNet-50 上以自建代价模型求得可证明最优解需 63.9M 周期,而启发式规则仅需 20.9M 周期,差距被归因于代价模型本身存在偏差。文章进一步指出,Triton 的 RemoveLayoutConversions 等编译器决策依赖对闭源软件栈与硬件实际开销的不准确估计,这才是真正的难点。

报道正文

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 arbitrary1, 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 performance2.

The finding that layout search is computationally hard in Amazon’s paper is valid, and good to share with your executives3 . 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 kernel4. 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.