Roadmap

Layout laws

Scope

The laws of the layout library in Modular's kernels (max/kernels/src/layout/ and Mojo/stdlib/std/utils/coord.mojo, at commit bbc13a71): how a coordinate maps to an offset, how layouts divide, multiply, compose, coalesce and swizzle, and which layouts support stepping by a stride. Each card quotes the docstring or code line that states its law and says, from the code, where the law holds and what is out of its scope.

Items

Out of scope

Depends on

The Modular source at bbc13a71. Within the area, most laws build on crd2idx (the depends lines of the cards).

Coverage in MojoProof

MojoProof/Layout/ transcribes the layout functions over a model of nested integer tuples (docs/proofs/layout-laws.md gives the transcription and the laws) and proves laws of 15 cards for every shape that meets their premises: crd2idx (mixed coordinates included), idx2crd, cosize, reverse-transpose, zipped-divide, blocked-product, coalesce, composition (for a b of any number of modes that meet composeDiv and whose terms do not carry), upcast, swizzle, bit-layout (MojoProof/Layout/Bits.lean: BitLayout and the placement of a flat layout that is_bit_linear accepts), two-level-swizzle (MojoProof/Layout/TwoLevel.lean: two_level_layout_from_swizzle is Swizzle(L(x)) on every flat layout that passes its asserts, at every coordinate inside the shape), is-affine (the stepping law for flat layouts), row-major-views and index-width. These 15 cards are L1. Where a law needs a precondition, MojoProofTest.LayoutLaws has a counterexample without it, by decide on the model: composition, blocked product, idx2crd, zipped divide, upcast, the swizzle, the placement and the two-level swizzle. MojoProofTest.LayoutConformance compares the model with the values that Mojo's compiler folds into a kernel's LLVM IR for random static layouts and the counterexamples' inputs; they agree on all 43 cases, 351 values (tests/layout/record.txt). BitLayout and two_level_layout_from_swizzle are not in that test. MojoProofTest.LayoutRuntime compares the model with a CPU run of a compiled program on 9 layouts with runtime leaves (offsets, sizes and cosizes, the runtime upcast among them); they agree on 8, and on the ninth, at scalars past the size 0 of (0, 2):(1, 10), Mojo's remainder by 0 is 0 where the model's keeps the dividend (tests/layout/runtime/record.txt). Both are tests of the transcription on those cases, not proofs, and neither is a GPU run, so no card is past L1. No proved kernel uses the laws yet: the kernel proofs work on the compiled IR, where a layout has already become integer arithmetic.

Next steps

Status of the cards

From roadmap/status.yaml, which the roadmap script writes from the code.

CardKindLevelEvidence above the levelNextCorpus kernels
bit-layout: A layout whose extents are powers of two is linear over F2constructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
blocked-product: blocked_product's outer stride is documented as the block's cosize times the tiler's strideconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
coalesce: coalesce keeps the offsets and merges contiguous modesconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
composition: composition(a, b) maps a coordinate through b, then aconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
cosize: cosize is the offset of the last coordinate plus oneconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
crd2idx: crd2idx is the dot product of a coordinate with the stridesconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
idx2crd: idx2crd maps an offset back to a coordinate, documented as the inverse of crd2idxconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
index-width: With a narrow index type the caller must ensure products and sums fitconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
is-affine: Stepping by a stride is exact only for an affine layoutconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
reverse-transpose: reverse and transpose reverse the shape and the strideconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
row-major-views: TileTensor.as_span, reshape and coalesce need row-major contiguous stridesconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
swizzle: Swizzle is x ^ ((x & mask) >> shift) and needs abs(shift) >= bitsconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
two-level-swizzle: two_level_layout_from_swizzle evaluates to Swizzle(L(x))constructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
upcast: upcast divides strides and shapes by the factor with shape_divconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0
zipped-divide: zipped_divide splits a layout into tile and number-of-tiles modesconstructL1-L2 needs a recorded GPU run (a conformance test, not a proof)0