Docs / Proofs

The layout laws

This document states the laws of the layout library of Modular's kernels at modular/modular bbc13a71 (max/kernels/src/layout/ and Mojo/stdlib/std/utils/coord.mojo) that MojoProof proves about its Lean model of that library, one law per card of roadmap/layout/cards/. It gives the model, then each law with its premises and its scope, then the bugs that the model shows by a counterexample, then the transcription table and the conformance test that compares the model with the values the Mojo compiler computes.

The Lean statements are the reference. Every law below holds for all inputs that meet its premises unless its scope says otherwise; the concrete instances and the counterexamples are in MojoProofTest/LayoutLaws.lean. The model lives under MojoProof/Layout/ (namespace MojoProof.Layout), apart from the LLVM data layout MojoProof.Sem.Layout.

1. The model

MojoProof/Layout/Basic.lean.

MojoProof/Layout/Algebra.lean transcribes the functions that build one layout from others (coalesce, composition, zipped_divide, blocked_product, upcast) and the XOR swizzle Swizzle. The layouts they return have static leaves. MojoProof/Layout/Bits.lean transcribes BitLayout of bit_layout.mojo, a map over F2 from domain bits to codomain points, and is_bit_linear and placement on flat static layouts. It computes on natural numbers where Mojo computes on UInt64; the two agree while every image, span and offset is below 2 ^ 64. MojoProof/Layout/TwoLevel.lean transcribes TwoLevelLayout.__call__ of two_level_layout.mojo on a flat static outer layout, and two_level_layout_from_swizzle with its two asserts on flat static layouts; Mojo computes in Int, and the two agree while every offset is below 2 ^ 63.

2. The laws

Each law names its card, its Lean declarations and its premises. "Every shape" means every tree of the model, nested or flat, static or runtime, of any rank.

2.1 crd2idx (crd2idx)

2.2 idx2crd (idx2crd)

2.3 cosize (cosize)

2.4 reverse and transpose (reverse-transpose)

2.5 is-affine (is-affine)

TensorLayout.is_affine (layout.mojo:116-120) says whether a layout's offset is a sum of coordinate times stride; code that steps through memory by adding a stride must check it. Layout keeps the trait's default True, so Lay.isAffine is true on every layout of the model.

2.6 row-major views (row-major-views)

rowMajor ss is the flat layout of row_major(shape) (layout.mojo:1002): the last stride is 1 and each earlier stride is the product of the later extents.

Together: a row-major layout of positive extents is a bijection from its in-bounds coordinates onto the interval [0, size), so a span over it visits every element once. For every list of positive extents.

2.7 coalesce (coalesce)

2.8 composition (composition)

2.9 zipped divide (zipped-divide)

2.10 blocked product (blocked-product)

2.11 upcast (upcast)

2.12 swizzle (swizzle)

2.13 bit layout (bit-layout)

2.14 two-level swizzle (two-level-swizzle)

2.15 index width (index-width)

3. Counterexamples

Each is an example by decide in MojoProofTest/LayoutLaws.lean, on the model. The conformance test (section 5) compiles the same inputs, and the compiler's values are the model's, so each bug is in the code Mojo compiles, not only in the model. Each item gives the premise under which the law holds (section 2).

4. The transcription

Each model definition and the Mojo function it transcribes, at modular/modular bbc13a71.

Mojo file:line Lean (MojoProof.Layout.)
Coord node, ComptimeInt coord.mojo:186, :97 Tup
__len__ coord.mojo:125-131, :307 Tup.len
value() coord.mojo Tup.value
flatten coord.mojo:524 Tup.leaves, Tup.leavesL
product coord.mojo:368 Tup.size
crd2idx coord.mojo:856 (hierarchical 899-904, flat 908 via _crd2idx_flat :795, scalar 914-935, leaf 936-953) crd2idx, zipIdx, splitIdx
idx2crd coord.mojo:958, :1067; _linear_idx_to_coord :1966; static 1 at 1037-1042 idx2crd, idx2crdL
Layout layout.mojo:329 Lay
TensorLayout.is_affine layout.mojo:116-120 (Layout keeps the default, :329) Lay.isAffine
Layout.__call__ layout.mojo:484 (513-538) Lay.call
Layout.size, product layout.mojo:616-635 Lay.size
Layout.cosize layout.mojo:639-653 Lay.cosize
Layout.reverse, transpose layout.mojo:656-667, :670 Lay.reverse
row_major(shape) layout.mojo:1002 (strides 1029-1048) rowMajor
_shape_div, _comptime_shape_div, _runtime_shape_div layout.mojo:1501, :2094, :2199 shapeDiv
_compose_leaf layout.mojo:1509 (loop 1533-1545, last mode 1546-1551) composeStep, composeLeaf
composition, _compose_mode layout.mojo:1641, :1556 Lay.compose
zipped_divide layout.mojo:1407 (1427-1432, 1457) Lay.zippedDivide
blocked_product layout.mojo:1954 (BlockedProductLayout :1884; size at 1995, 2016) Lay.blockedProduct
upcast layout.mojo:2218 (2263-2296) Lay.upcast
_CoalesceReducerIdx, coalesce layout.mojo:2316-2358, :2462 (start 2380) coalesceStep, Lay.coalesce
shiftr swizzle.mojo:236 shiftr
Swizzle, yyy_mask, __call__ swizzle.mojo:274, :337, :369-381 Swizzle, Swizzle.mask, Swizzle.call
BitLayout, BitLayout() bit_layout.mojo:66-82, :84-88 BitLayout, BitLayout.empty
put bit_layout.mojo:90-104 BitLayout.put
BitLayout.__call__ bit_layout.mojo:168-182 BitLayout.call, xorBits
bits bit_layout.mojo:52-62 bitsMask
_log2, _power_of_two bit_layout.mojo:222-223, :226-227 log2 (trailingZeros), powerOfTwo
is_bit_linear bit_layout.mojo:230-258 isBitLinear, bitLinear
placement bit_layout.mojo:261-283 placement, placeModes, placeMode
_TwoLevelLayout, _InnerImage, _InnerBitCount two_level_layout.mojo:471-631, :86, :63 TwoLevel
_FullShapeTabulator two_level_layout.mojo:240-248 TwoLevel.shape
TwoLevelLayout.__call__ two_level_layout.mojo:633-698 (scalar 666-680, inner bits 684-691, result 697) TwoLevel.call, TwoLevel.offset, innerXor
_SwizzlePeriod, _SwizzleInnerBits two_level_layout.mojo:956-959, :962-976 (_log2_or_zero :140) Swizzle.period, Swizzle.innerBits
two_level_layout_from_swizzle, SwizzledTwoLevelLayout, _SwizzleOuter, _SwizzleInner two_level_layout.mojo:1101-1133, :1049-1064, :1015-1047, :978-1013 Swizzle.twoLevel
_swizzle_layout_valid, _swizzle_inner_bits_disjoint two_level_layout.mojo:1066-1078, :1081-1098 swizzleValid, Swizzle.disjoint

The transcription is trusted: no proof connects these definitions to the Mojo source. The conformance test is the check that it does connect them on the recorded cases.

5. The conformance test

tests/layout/ holds a Mojo program generated from a seed by tests/layout/gen.py: a GPU kernel that stores the values of layout functions on random static layouts (offsets, cosizes, coalesced offsets, the offsets and sizes of zipped divides, blocked products and compositions, upcast shapes and strides, swizzled values, idx2crd coordinates) into its output, with the inputs of the counterexamples among them. It is compiled to LLVM IR, never run; the compiler evaluates the static layout functions, so the values are constants in the stores of the kernel's IR. tests/layout/extract.py reads them and writes MojoProofTest/LayoutConformance.lean, which checks by decide that the model gives the same values. tests/layout/record.txt records the shapes, the seed, the Mojo version and the result. The kernel's IR is kept as tests/layout/conformance.nvptx.ll, so the check can be rerun without Mojo.

The result at seed 1729, Mojo 1.3.0.dev2026111705 for sm_80: the model agrees with the compiler on all 43 cases (351 values). In particular the compiler gives the counterexamples' values: composition((6,2):(1,6), (3):(4)) has the offsets 0, 6, 12; composition((2,2):(1,10), (2,2):(1,1)) the offsets 0, 1, 1, 2 and composition((6,2):(1,100), (3,2):(2,3)) the offsets 0, 2, 4, 3, 5, 7; blocked_product((2):(2), (2):(1)) the offsets 0, 2, 2, 4; idx2crd maps 3 in (2,2):(3,1) to (1,1); zipped_divide[2]((5):(1)) has size 4; Swizzle(2, 0, 1) maps 6 to 5 and 5 to 7; and upcast[2]((3):(1)) is (1):(1). The instances agree too: the tile of row_major[4, 8] has the offsets 0, 8, 2, 10, 4, 12, 6, 14, upcast[2] of row_major[4, 8] is (4,4):(4,1), and the mixed coordinates (1, 5) and (1, (1, 2)) of (2,(2,3)):(1,(2,4)) have the offset 11.

This is a test, not a proof: it covers the recorded cases only, all with static leaves, and the program is compiled, never run, so it says nothing of layouts with runtime leaves (which the compiler does not fold) or of the GPU.

Layouts with runtime leaves have their own test, in tests/layout/runtime/: a CPU program, runtime.mojo, builds 9 layouts whose extents and strides are Int64s offset by a command-line argument, so the compiler cannot fold them (c3 mixes in the static Idx[2] and Idx[1]), and prints each one's offsets, size and cosize. Mojo 1.3.0.dev2026111705 compiles it, the CPU runs it with the argument 0, and tests/layout/runtime/extract.py writes MojoProofTest/LayoutRuntime.lean from the printed values; tests/layout/runtime/record.txt records the run. The model agrees with the run on 8 cases (79 values): flat (3,4):(1,3), (2,3,2):(6,1,0) with a stride 0, nested (2,(2,3)):(1,(2,4)), the mixed (2,3):(3,1), and the runtime upcast (_runtime_shape_div) of row_major[4, 8] (the offsets of (4,4):(4,1)), of (3):(1) (size 1, no assert), of (4,3):(3,1) and of (4):(0) (size 0, cosize 1). It differs on one: at the scalars 1 and 5 of (0,2):(1,10), past the size 0, Mojo gives the offset 0 and the model 1 and 5, since Mojo's remainder by 0 is 0 and the model's keeps the dividend (section 1, division by zero); MojoProofTest/LayoutRuntime.lean states the model's values there and quotes the run's. This too is a test, not a proof: one run of these 9 layouts on one CPU, nothing about other shapes or the GPU.