Tensor Seeks Layout: Formalizing Layout Selection for ML Compilers
Organizations: TU Wien, Vienna, Austria · Amazon, Cupertino, California, USA
Abstract
Modern machine learning compilers select tensor memory layouts to minimize execution cost under hardware constraints. Layout selection is global: an operator may be fastest under one layout while its consumers prefer another, and aligning these preferences requires explicit layout conversions that can hurt model performance. Despite its practical importance, layout selection lacks a formal basis, so current compilers rely on ad-hoc heuristics. This paper presents the first formal study of layout selection in machine learning compilers. We formulate the problem as combinatorial optimization over dataflow graphs, minimizing the sum of operator execution costs and the per-tensor cost of these conversions. Our theoretical analysis shows that optimal layout selection is computationally hard, even for programs containing only matrix multiplications over two-dimensional tensors. We design an optimal polynomial-time algorithm for dataflow graphs of bounded treewidth. For general instances, we give a weighted MaxSAT encoding that an off-the-shelf solver can optimize. The formulation unifies several existing layout optimization strategies, including XLA's layout assignment, partition dimension selection in systolic array compilers, and layout planning in mobile GPU optimizers. We implement the formalization in a production compiler for an AI accelerator and measure the execution time of the compiled models under greedy heuristics, the compiler's rule-based strategy, and an optimal solver. Simple heuristics degrade execution time by up to on some workloads. Where the compiler's cost model is accurate, the solver matches or beats the rule-based strategy. On workloads with complex data movement it falls behind, and since the solver minimizes the stated objective exactly, that gap isolates cost-model error from search quality, showing where compiler effort actually pays off.
Figures & tables
func f(A,B) computing , then , then , returning~. Panels~(b) and~(c) show the same dataflow graph twice, with operators as boxes, tensors as labelled arrows, and each tensor's element grid drawn beside it in storage order. In~(b) every tensor is row-major: inputs and feed matMul, producing ; reduce in its RM form produces ; add combines with (broadcasting over the rows of ) to give , and no conversion is needed. In~(c) the inputs arrive transposed as and , so matMul yields , the cheaper CM form of reduce yields , and add yields ; a final transpose operator, shaded differently from the compute operators, converts back to .NHWC), batch norm (prefers NCHW), add (accepts NHWC or NCHW), and transpose. The two remaining panels each show the same graph: a single Input node fanning out to a row of alternating convolution and batch-norm operators (an ellipsis marks the omitted middle ones), which all fan back in to one shared Add that feeds Output. In the locally-optimal assignment~(b), each operator takes its own preferred layout, so the convolutions are NHWC and the batch-norms NCHW; because the shared Add runs in NHWC, a transpose box is inserted on the outgoing edge of every batch-norm. In the globally optimized assignment~(c), every operator including the batch-norms runs in NHWC, so all edges reach the Add directly and no transpose boxes appear at all.func f(): first one source operator per vertex, producing tensors , , , , each with layout drawn from ; then one matmul per edge, , , , and , each annotated with the edge it encodes. Two trailing comments record the costs: a matmul costs if and only if its two input layouts differ, and the transpose cost between and is in either direction.mm, whose result feeds the reduction red, whose result feeds add; also feeds add directly by a long edge on the right, and add produces . Panel~(b) draws a nice tree decomposition of width~, so every bag holds at most three elements. The root is a leaf-labelled bag ; below it an introduce `add`'' step gives $\{B,\texttt{add}\}$, which is a join node with two children. The left branch goes $\{B,\texttt{add}\}$, then forget , introduce add'' to , then forget $R$'' to the leaf $\{\texttt{add}\}$. The right branch goes $\{B,\texttt{add}\}$, then introduce red'' to , then forget `add`, introduce `mm`'' to $\{B,\texttt{red},\texttt{mm}\}$, then forget red, introduce '' to , then forget $A$'' to $\{B,\texttt{mm}\}$, then forget mm'' to the leaf .| Type | Model | Local | Greedy | Rule-Based | Solver-Based |
| Dense Transformers | ALBERT-large | 53.4 | 45.8 | 44.7 | 45.3 |
| BERT-base | 19.2 | 19.3 | 18.5 | 18.5 | |
| BERT-large | 29.0 | 29.6 | 28.7 | 28.7 | |
| BART-base | 2.4 | 2.6 | 2.4 | 2.4 | |
| DistilBART | 2.2 | 2.3 | 2.2 | 2.1 | |
| Electra-large | 23.2 | 23.7 | 23.0 | 23.0 |
Appendix figures & tables1 asset
Supplementary material from the paper’s appendix.