Docs / Proofs

Tensor cores: fragments, ldmatrix and mma (Frag, Frag.bijOn, fragA_inv, fragB_inv, fragC_inv, ldm_inv, quad_inv, mmaFrags_inv, ldmatrix_gather, ldmatrix_fragA, ldmatrix_fragB_cols, ldmatrix_fragB, ldmatrix_fragBt, mmaStep_gather, mma_exact, mma_contract, castDot_ok, fuseAcc_ok, gridAcc_ok, seqAcc_ok, Grid.warpReady, mmaWarp_spec, ldmWarp_spec, Grid.fire_mma)

Lean: MojoProof/Sem/TensorCore.lean, for the fused accumulation MojoProof/Sem/TensorCoreAcc.lean, for the steps on run-time values and the grid's step MojoProof/Sem/TensorCoreWarp.lean, and the definitions the grid runs in MojoProof/Sem/TensorCoreDef.lean; instances and twins in MojoProofTest/TensorCore.lean, MojoProofTest/TensorCoreWarp.lean and MojoProofTest/TensorCoreGrid.lean. Most statements are about functions of the 32 lanes' registers and arguments. The grid runs llvm.nvvm.mma… with bf16 and f16 inputs and an f32 accumulator (Grid.fire_mma, "The grid's step"); it reports llvm.nvvm.ldmatrix… and every other mma unsupported. See "Not proved".

The fragment model

The source is the PTX ISA, version 9.5: section 9.7.16.5.14 (mma, its "Matrix Fragments for mma.m…n8k…" tables in sections 9.7.16.5.1-4 and 9.7.16.5.6-11) and section 9.7.16.5.15 (ldmatrix), as docs/TRUST.md T14 reads them. The repository holds no copy of the ISA, so the citation is by section; the layouts below are the tables' formulas, written with g = l / 4 (the ISA's groupID) and t = l % 4 (threadID_in_group) for lane l.

A fragment layout F : Frag places an F.rows × F.cols tile in the registers of a warp: each of the 32 lanes holds F.regs registers (32-bit, and 64-bit for f64), each holding F.pack elements, and F.pos l r s is the tile cell (row, column) of element s of register r of lane l (element 0 in the low bits). F.own i j is the inverse, the (lane, register, element) holding cell (i, j). F.Inv says the two are inverse on the 32-lane domain and the tile, and Frag.bijOn turns that into Mathlib's Set.BijOn: pos is a bijection from {(l, r, s) | l < 32, r < regs, s < pack} onto {(i, j) | i < rows, j < cols}. A tile of any type is put into registers by F.scatter T l r s = T (F.pos l r s) and read back by F.gather x i j = x (F.own i j); on an Inv layout, registers that agree with scatter T on the lanes' registers gather to T on the tile (Frag.gather_of_scatter), so gather (scatter T) = T there (Frag.gather_scatter). The loads below are stated through gather, the way mma reads its operands.

The families, for h blocks of 8 rows, p elements per register and c chunks, K = 4 p c:

layout rows × cols regs pos l r s
fragA h p c (A, row major) 8h × K hc (g + 8 (r % h), s + p (t + 4 (r / h)))
fragB p c (B, column major) K × 8 c (s + p (t + 4 r), g)
fragC h p (C and D) 8h × 8 2h / p (g + 8 (i / 2), 2 t + i % 2), accumulator i = p r + s
ldm q false (ldmatrix.x<q>) 8q × 8 q (8 r + g, 2 t + s)
ldm q true (ldmatrix.x<q>.trans) 8q × 8 q (8 r + 2 t + s, g)

The ISA's shapes are instances: m16n8k8 .tf32 is fragA 2 1 2, fragB 1 2; m16n8k16 .bf16 and .f16 are fragA 2 2 2, fragB 2 2; m16n8k4 .tf32 is fragA 2 1 1, fragB 1 1; m16n8k8 .bf16 and .f16 are fragA 2 2 1, fragB 2 1; all with the f32 accumulator fragC 2 1, or the f16 one fragC 2 2 (two to an .f16x2 register, sections 9.7.16.5.7 and 9.7.16.5.8). In f64, one element to a 64-bit register, m16n8k<4c> is fragA 2 1 c, fragB 1 c, fragC 2 1 (the ISA's f64 tables at m16n8k4 and m16n8k8 are its tf32 ones), and m8n8k4 is fragA 1 1 1, fragB 1 1, fragC 1 1 (section 9.7.16.5.2). With 8-bit and 4-bit inputs, four and eight to a register (sections 9.7.16.5.3-4 and 9.7.16.5.9-11), m16n8k32 is fragA 2 4 2, fragB 4 2 in 8 bits and fragA 2 8 1, fragB 8 1 in 4 bits; m16n8k16 in 8 bits is fragA 2 4 1, fragB 4 1; m16n8k64 in 4 bits is fragA 2 8 2, fragB 8 2; m8n8k16 and m8n8k32 are fragA 1 4 1, fragB 4 1 and fragA 1 8 1, fragB 8 1; the s32 accumulators are fragC 2 1 and fragC 1 1. The importer reads no such mma. mmaFrags v maps the importer's Mma to these three layouts for every shape m16n8k<k> with a tf32, bf16 or f16 input and an f32 accumulator, or in f64, k a positive multiple of 4 p (p = (abForm v.ab).pack), and for m8n8k4 .f64. m8n8k4 with f16 inputs, whose layouts (section 9.7.16.5.1) are not these families, gets none, and since the importer reads no f16 accumulator, mmaFrags never gives fragC 2 2. That mma is four products at once: the quad pair q = l % 16 / 4, lanes 4q to 4q + 3 and 4q + 16 to 4q + 19, multiplies its own 8 × 4 A and 4 × 8 B tiles into its own 8 × 8 accumulator. quadA t, quadB t and quadC p place the four tiles stacked (product q in rows 8 q to 8 q + 7 of A and C/D and 4 q to 4 q + 3 of B), with u = 4 (l / 16) and two elements to a register except the f32 accumulator:

layout rows × cols regs pos l r s
quadA false (A, row major) 32 × 4 2 (8 q + u + t, 2 r + s)
quadA true (A, column major) 32 × 4 2 (8 q + u + 2 r + s, t)
quadB false (B, column major) 16 × 8 2 (4 q + 2 r + s, u + t)
quadB true (B, row major) 16 × 8 2 (4 q + t, u + 2 r + s)
quadC 2 (C and D, f16) 32 × 8 4 (8 q + u + t, 2 r + s)
quadC 1 (C and D, f32) 32 × 8 8 (8 q + u + l % 2 + 2 (r % 4 / 2), 4 (r / 4) + 2 (t / 2) + r % 2)

The product of stacked tiles is block diagonal, so mmaStep does not model this mma. For ldmatrix, the tile of ldm q t is the q matrices of 8 × 8 16-bit elements stacked: row 8 m + i is row i of matrix m, whose address lane 8 m + i gives; .x1 uses lanes 0-7, .x2 lanes 0-15, .x4 all 32.

These are the conventions of GPUProof's PTX front end (its aRow/aCol, bRow/bCol, cRow/cCol and its ldmatrix destinations), restated here without depending on it. Where we differ: lanes, registers and elements are numbers with bounds rather than Fin and bit vectors; one theorem covers each family for every p, c and q, where the front end checks shapes one by one (its exec_tileA and exec_tileBt are ldmatrix_fragA and ldmatrix_fragBt at c = 2); .trans has a theorem here (ldm_inv q true, ldmatrix_fragB_cols, ldmatrix_fragB) and none in the front end; and the front end cites PTX ISA 9.4 where we cite 9.5.

Statements

  1. Frag.bijOn, for every layout: if F.Inv, pos is a bijection from the domain onto the tile (Set.BijOn).
  2. fragA_inv, fragB_inv: for every h, p and c, (fragA h p c).Inv and (fragB p c).Inv (for p = 0 the tile and the registers are empty); fragC_inv: for every h and p 1 or 2, (fragC h p).Inv (the twin fragC_three: not for p = 3); ldm_inv: for every q and both values of .trans, (ldm q t).Inv; quad_inv: for both values of the flag and p 1 or 2, (quadA t).Inv, (quadB t).Inv and (quadC p).Inv. The tests check by decide, for every lane, that the layouts at m16n8k8 .tf32, m16n8k16 .bf16/.f16 and the f32 accumulator put each element where the ISA's tables, written in their own form, put it (tableA8, tableB8, tableA16, tableB16, tableC), and likewise the f16 accumulator (tableC16), m8n8k4 .f64 (tableA64, tableB64, tableC64), A of m16n8k4 (tableA4) and m16n8k16 .f64 (tableA16d, tableB16d), the 8-bit and 4-bit inputs (tableA32s8, tableB32s8, tableA16s8, tableB16s8, tableA8s8, tableA64s4, tableB64s4, tableA32s4, tableB32s4, tableA8s4) and m8n8k4 with f16 inputs (tableQA, tableQB, tableQC, the quad pairs written as the ISA's lane ranges); bijA16 is Frag.bijOn at fragA 2 2 2.
  3. mmaFrags_inv: for every importer Mma value v that mmaFrags accepts (exactly those of Mma.table, the one finite list of shapes and types that the importer's Mma.ok reads too; the layouts' formulas stay general in m, k and the element count), the three layouts are Inv, A is v.m × v.k, B is v.k × 8, C is v.m × 8, and their register counts are the importer's Mma.regs v (regs16: (4, 2, 4) at m16n8k16; regs64: (1, 1, 2) at m8n8k4 .f64).
  4. ldmatrix_gather: for every count q, flag t, lane addresses a and memory reader rd, the registers after ldmatrix q t a rd, read in the layout ldm q t, hold rd (a R + 2 C) at every cell R < 8q, C < 8: the stacked tile at the rows' addresses.
  5. ldmatrix_fragA: for every c, ldmatrix.x<2c> without .trans loads the A operand of a 16-bit mma ((fragA 2 2 c).gather of the registers is A) when the matrices are A's 8 × 8 blocks in column-block order (rows 0-7, then 8-15, of each 8 columns); ldmatrix_fragB: ldmatrix.x<c>.trans of B's c blocks of 8 rows, stored row major, loads B ((fragB 2 c).gather of the registers is B), the two layouts putting every element at the same cell; ldmatrix_fragBt: ldmatrix.x<c> without .trans of B's transpose, matrix m its columns 8 m to 8 m + 7, loads the same B (the front end's exec_tileBt at c = 2). ldmatrix_fragB_cols, for every c and h: ldmatrix.x<ch>.trans of an 8c × 8h B tile stored row major, matrix m its block at rows 8 (m % c) and columns 8 (m / c), loads h B operands side by side, registers c o to c o + c - 1 gathering to columns 8 o to 8 o + 7; ldmatrix_fragB is its case h = 1. The tests: rows16 is ldmatrix_gather at .x4, loads16 gives the next three at m16n8k16 with concrete addresses, and loadsB2 the last at c = h = 2, the .x4.trans load of a 16 × 16 B tile as two operands.
  6. mmaStep_gather, for all layouts, K, element types and accumulation functions acc: when A, B and C are Inv and the shapes fit (C.rows ≤ A.rows, C.cols ≤ B.cols, K ≤ A.cols, K ≤ B.rows), the D tile of mmaStep on the scattered tiles TA, TB, TC is, at every cell (i, j) of C, acc (TA i ·) (TB · j) (TC i j).

The warp-collective step

mmaStep FA FB FC acc a b c is the D registers of every lane: lane l's register r, element s, is acc of row i of the A tile gathered from all lanes' a, column j of the B tile gathered from b, and C (i, j), where (i, j) = FC.pos l r s. Each output depends on other lanes' registers, so the step is collective: the ISA's .sync.aligned requires every lane of the warp to execute the same mma (or ldmatrix). Grid.warpReady g size n i states that obligation in the grid's terms, as Grid.fire checks it for shfl.sync: thread n's warp is whole inside its block of size threads and each of its 32 lanes waits at a call of intrinsic i at the same instruction. A lane that has returned, or waits elsewhere, breaks it.

On run-time values (MojoProof/Sem/TensorCoreWarp.lean), an element is its bit pattern and a register holds its elements in one of three forms (RegForm): word w, one w-bit scalar Val.bits w (tf32 in an i32, f64 in a double, the float and double accumulators); pair, two 16-bit elements in an i32, element 0 in the low half (bf16, and ldmatrix's b16); vec2, the two lanes of a <2 x half> (f16). RegForm.get reads element s of a Val, none for poison, undef, a pointer or another width or shape, and RegForm.put builds the register. For every form, a built register reads back as its elements modulo 2 ^ width (RegForm.get_put), and a register of the form is rebuilt from its elements (RegForm.put_elem).

MojoProofTest/TensorCoreWarp.lean instantiates both at two shapes (m16n8k16 .bf16 and m16n8k8 .tf32; .x4 and .x2) and runs them on registers written by hand from the ISA's tables.

The grid's step

Grid.fire's mma arm (MojoProof/Sem/Grid.lean) is the step of the warp of thread n: the warp must lie whole in its block of threads, and mmaRes reads the 32 lanes' statuses. A lane that waits at another collective or another instruction, or has returned, is unsupported; while a lane runs the result is none; when all 32 wait at the same mma at the same instruction, mmaGrid L.acc v runs mmaWarp v on their arguments with the launch's accumulation policy L.acc on bit patterns (L.acc.bits fi, "Accumulation policies"), and each lane resumes with its aggregate of D registers under its callee's return attributes, the memory unchanged. The inputs are bf16 and f16 (gridFmt gives their formats fi) with an f32 accumulator; a call whose words are not all finite, a shape Mma.table does not list, and every other input or accumulator type are unsupported.

Grid.fire_mma: for every launch L, so every accumulation policy L.acc, every warp and every mma of the table with bf16 or f16 inputs and an f32 accumulator (mmaFrags v = some (FA, FB, FC), gridFmt v.ab = some fi, v.cd = .float): when the warp lies whole in its block and each lane j < 32 waits, from thread u j, at the mma at the instruction of thread n, with arguments that encode the tiles TA, TB, TC of finite words in the layouts, and with a callee d j none of whose return attributes is a range, nonnull or align, the step succeeds: g.fire L c n = .ok (some g'), where g' has the memory of g, every thread outside the warp unchanged and lane j running (u j).resume (some (y j)), and y read in FC is L.acc.bits fi (TA i ·) (TB · j) (TC i j) at every cell. The premise on the return attributes is the weakest the step allows: those three are the attributes withAttrs refuses on an aggregate (a fault, "nonnull or align on a non-pointer" for the latter two), and every other attribute passes the aggregate of words through. No premise is on the policy. The lanes' argument functions in the grid (xs.getD l []) and in mmaWarp_spec (encode … l) agree on the 32 lanes only; mmaWarp reads no other lane, which the private lemma mmaWarp_lanes checks on the concrete layouts.

MojoProofTest/TensorCoreGrid.lean instantiates it at m16n8k16 with f16 inputs and at m16n8k8 with bf16 inputs, and runs one program (one block of 32 threads, every lane passing the same registers) at both shapes, whose stored words are the sums 16.5 and 32.5 (f16) and 8.5 and 16.5 (bf16); the same program with one lane at the other mma is unsupported, and with the intrinsic declared with a nonnull return it faults. A warp waiting at m16n8k24 .bf16, a shape the table does not list, with registers of Mma.regs' counts, is unsupported, and one at m16n8k8 .bf16 steps; with f16 inputs, a warp at m16n8k32, another shape the table does not list, is unsupported, and one at m16n8k16 steps, each with registers of its own counts and forms.

Accumulation policies

The PTX ISA (version 9.5, section 9.7.16.5.14) guarantees of mma's accumulation only that it is performed with at least single precision; its order, its rounding and its handling of subnormal inputs are unspecified. So the semantics does not fix one: a policy (MmaAcc, in MojoProof/Sem/TensorCoreDef.lean) is a function that, for every input format fi, accumulator format fc and number K of products, gives the D word from the K input pairs and the C word, over FloatLib's values. Each launch carries one (Launch.acc, gridAcc by default), and Grid.fire runs the launch's. Every statement about a grid with an mma is quantified over the launch, so it holds for every policy; one that needs a property of the D words takes a contract on L.acc as a premise, and the premise is only what its proof uses.

mma_contract takes the contract AccBy mag magC bnd acc and is instantiated at both policies, at m16n8k16 .bf16 (contract16 with seqAcc and bf16_ok, contractFuse with gridAcc and fuse16_ok). Grid.fire_mma and mmaWarp_spec need no contract: they give the D words as the policy's value at every cell. One policy covers a whole launch, every warp and every shape; a device whose accumulation depends on the shape, the warp or the call is a policy of the launch only if it is a function of the formats, the products and C.

The accumulation model

The ISA does not specify the arithmetic inside mma ("Accumulation policies"); the internal layout of .tf32 is implementation defined. FloatLib has no fused multi-term accumulator (its blocked dot products are chains of fused multiply-adds), so the accumulation is a parameter acc of the step, with three models.

That a GPU's accumulation is a given policy, or meets AccBy for some magnitudes and bound, is a trust item (a reading of the ISA's precision text, which gives no bound; docs/TRUST.md T14). A statement about a grid covers a GPU for every policy; one that takes a contract covers it only if the GPU's accumulation meets the premise. The GPU runs below support, without proving it, that an A100's m16n8k16 .bf16 accumulation is gridAcc, which meets its contract by gridAcc_ok. The bit-level model of one GPU's accumulation in GPUProof's PTX front end is another candidate, and it is not used here.

GPU runs

tests/gpu/mma/record.txt is a conformance test, not a proof: one warp's mma.sync.aligned.m16n8k16.row.col.f32.bf16.bf16.f32 on an NVIDIA A100 (sm_80), 20 launches on registers from a seed (random inputs of nine kinds and two fixed probes), the 32 lanes' A, B, C and D registers dumped. MojoProofTest/TensorCoreGpu.lean (run by tests/gpu/mma/Compare.lean) evaluates mmaStep (fragA 2 2 2) (fragB 2 2) (fragC 2 1) on the dumped registers with gridAcc .bfloat16 .binary32 (printed as fuseAcc), with seqAcc .bfloat16 .binary32 (printed as bfAcc; bf16_ok puts it within dotBound .binary32 16) and with the exact rational sum, and compares each D word; dotBoundQ_eq says that its rational bound is dotBound .binary32 K.

tests/gpu/ldmatrix/record.txt is a second conformance test: one warp's ldmatrix.x4 and ldmatrix.x4.trans on the A100, 8 launches, each from 512 random 16-bit words of shared memory at row addresses from the seed (in order in the first launch, random multiples of 16, repeats allowed, in the others). ldmCompare (MojoProofTest/TensorCoreGpu.lean, run by tests/gpu/ldmatrix/Compare.lean) compares every element of the dumped registers with ldmatrix 4 false and ldmatrix 4 true on the dumped words and addresses: all 4,096 agree. So on these inputs the hardware's ldm 4 t is the library's, plain and transposed.

Not proved