A program with contraction off runs as the program under a never-fusing oracle
Statement
Module.contractOff is a module with the contract flag removed from every float binary operation
(fadd, fsub, fmul, fdiv, frem) of every function; nothing else changes (calls, fneg,
fcmp and their flags are kept). Func.contractOff does the same to one function. neverFuse o
is the oracle row whose number at position p is 3 * (o p / 3): the same NaN choice (floatBin
delivers at n / 3) and the remainder 0, at which contract never fuses.
Theorem (Kernel.Sound.contractOff, MojoProof/Sem/ContractOff.lean). For every program P
whose only statements that read an oracle number are float binary operations
(Program.fbinOracle: no fpext or fptrunc instruction in any function, and no maxnum,
minnum or rsqrt.approx intrinsic), every kernel fn with no fpext or fptrunc instruction
(Func.fbinOracle), every program Q with the module P.module.contractOff, the layout of P
and the intrinsics of P (the digest and the kernel names may differ), every launch, argument
values, memory, admissibility adm, observation and relation R:
Kernel.Sound P fn L args m adm obs R →
Kernel.Sound Q fn.contractOff L args m (fun ch ω => adm ch fun n => neverFuse (ω n)) obs R
So every run of the contraction-off kernel under the oracle family ω is, step for step, a run of
fn in P under the rows neverFuse (ω n), with the same memory at every step: the same faults,
trace, races and final memory, hence the same observation.
Theorem (Kernel.Sound.contractOff_iff, the converse). For the same P, fn, Q, launch,
arguments, memory, admissibility adm, observation and relation R:
Kernel.Sound Q fn.contractOff L args m adm obs R ↔
Kernel.Sound P fn L args m (fun ch ω' => ∃ ω, adm ch ω ∧ ω' = fun n => neverFuse (ω n)) obs R
The contraction-off kernel is sound for adm exactly when fn is sound at the never-fusing rows
of the families adm admits. The right-to-left direction is Kernel.Sound.contractOff (an
admitted ω gives its own rows); left to right, every run of P under the rows neverFuse (ω n)
is, mapped, a run of Q under ω.
Theorem (Kernel.Obs.contractOff_iff, MojoProof/Sym/ContractOff.lean). For the same P,
fn, Q, launch, arguments, memory, observation, address choices ch and oracle family ω, and
every value y: y is the observation of a complete run (Kernel.Obs) of fn.contractOff in
Q under ω exactly when it is one of fn in P under the rows neverFuse (ω n).
Kernel.Obs.contractOff is its left-to-right direction.
Corollary at every row stride (NaiveOff.soundF, MojoProofTest/NaiveOff.lean). For every
row strides sA, sB, sC of the naive family (NaiveFam.prog sA sB sC,
MojoProofTest/Family.lean), every program Q and kernel F that are the family's compiled with
contraction off (Q's module is (NaiveFam.prog sA sB sC).module.contractOff, its layout and
intrinsics are the family's, and F is (NaiveFam.fn sA sB sC).contractOff), every oracle
family ω, launch valid with z sides 1, sizes m, n, K below 2 ^ 31, blocks, tensors
A (m × sA), B (n × sB) and memory of Mat.Pre at those strides (the premises of
NaiveFam.sound): F in Q is sound for Elem.Out of the family's bytes at the rows
neverFuse (ω k), at the cells NaiveFam.cell sC. It is NaiveFam.sound at those rows, moved by
Kernel.Sound.contractOff. The family's program and kernel are of the fragment at every stride
(progF_fbinOracle, fnF_fbinOracle), by the kernel's evaluation under the binders
(kernel_rfl): the strides are the constants of three mul instructions, which the check does
not read. By dotLoop_neverFuse (MojoProof/Sem/Dot.lean: dotLoop true at the numbers
3 * (n / 3) is dotLoop false at the numbers n), those bytes put at every cell in bounds the
binary32 encoding of dotLoop false (o (ω k)) from +0 over its pairs, the unfused fold
(bytes_neverFuseF).
Corollary at every row stride (NaiveOff.matmul_nanEqF, NaiveOff.matmul_errorF). For the
same strides, Q, F, oracle family ω, launch, sizes, K ≤ sA, K ≤ sB, blocks, tensors and
memory: in every observation of a complete run of F in Q under ω, every cell (r, c) in
bounds and in the launch's window holds, at offset (r * sC + c) * 4, the binary32 encoding of a
word v that is NaNEq to entry (r, c) of TorchLean's Tensor.matmul of the first K
columns of A and the transpose of the first K columns of B, with no premise on the oracle;
when v is finite, it is within dotBound of the real product on those columns. Every such
observation is one of NaiveFam.fn sA sB sC under the rows neverFuse (ω n)
(Kernel.Obs.contractOff), whose fadd numbers are multiples of 3 and fuse no round, so
NaiveFam.matmul_nanEq and NaiveFam.matmul_error at those rows apply, the first with its
premise met.
The corpus kernel (NaiveOff.correct, NaiveOff.bytes_neverFuse, NaiveOff.matmul_nanEq,
NaiveOff.matmul_error). The corpus kernel 16826c8c of the directory mgemm_nocontract is the
family's at 768, 768, 50257 compiled with contraction off (corpus_off): by evaluation in
the kernel (rfl), its module is naive.prog.module.contractOff (module_eq), its kernel
naive.fn_0.contractOff (fn_eq), and its layout and intrinsics are naive's (layout_eq,
intrinsics_eq), naive being the family's at those strides (NaiveFam.prog_corpus). Each of
the four is the corollary above at those strides, at every oracle family, launch valid with z
sides 1, sizes, blocks, tensors and memory of Mat.Pre (the premises of NaiveKernel.correct,
whose bounds give the sizes below 2 ^ 31, NaiveSpec.dims_lt), K ≤ 768 for the last two;
correct_unfused is NaiveOff.correct with the bytes of bytes_neverFuse. By
decide +kernel, naive and its kernel are of the fragment (naive_fbinOracle,
fn_fbinOracle).
Scope
The transport is proved for every program of the fragment Program.fbinOracle and every kernel of
Func.fbinOracle, in both directions: Kernel.Sound and the observations of complete runs move
from P to the contraction-off program and back, at the never-fusing rows. The converse holds
exactly at the image of neverFuse: by Kernel.Obs.contractOff_iff, the observations of Q
under ω are those of P under the rows neverFuse (ω n). At a family with a fusing number it
fails: the twin ContractOff.twin (one program pair, one start) has an observation of P under
such a family that no complete run of Q under it has. The corollaries soundF, bytes_neverFuseF,
matmul_nanEqF and matmul_errorF are for every program and kernel that are the naive family's
at some row strides compiled with contraction off, at every launch valid with z sides 1, memory
of Mat.Pre at those strides with sizes below 2 ^ 31 and every oracle family; the corpus has one
such program, at 768, 768, 50257 (NaiveOff.correct and the others). matmul_nanEqF and
matmul_errorF and their instances cover the cells in the launch's window.
The choice of the oracle
The oracle number at position Nat.pair k i of a thread is read by the statement the thread runs
at its step k, and statements read it in different ways: floatBin takes the NaN choice
n / 3 and the fusion choice n % 3; floatCast (fpext, fptrunc) and rsqrt.approx take
the whole number, maxnum and minnum take n % 2 and n / 2. Three ways to name the oracle of
P:
- (a) Change
ωonly at the positions of flaggedfaddandfsubsteps. Which statement a thread runs at stepkdepends on the run (loads, branches), so this oracle is a classical choice over runs, and the step positions of every corollary would have to be proved again. - (b) Change every number the same way,
3 * (n / 3), and ask ofPthat its only oracle readers are float binary operations. The premise is a decidable property of the program's text: the naive kernel meets it bydecide +kernel. - (c) Relate the two runs' oracles statement by statement, inside the simulation: this is (a) stated as a relation, with the same dependence on the run.
The proof takes (b): one oracle family for every run and schedule, a premise checked by
evaluation, and an oracle whose fold for the naive kernel is a closed term (dotLoop_neverFuse).
No number 3 * (n / 3) fuses, whatever the recorded factors (floatBin_neverFuse), so the factor
record prods that fmul contract keeps in P's frames is never read; Frame.contractOff
clears it, and the contraction-off program records none.
Proof
- The operation (
floatBin_contractOff, withfloatBin_neverFuse): for every float type, operator, flags, words, factors and oracle numbern,floatBinwithfm.contractremoved atnisfloatBinwithfmat3 * (n / 3), whatever the factors on either side. - The statement (
exec_contractOff): for every statement of the fragment and every frame,execin a context ofQwith oracle rowoon the frame mapped byFrame.contractOff(the flags of its function removed, no factors) gives the effect, mapped byEff.contractOff, ofexecin the context ofPwith the scope's numbers3 * (ch i / 3). Operands read no oracle number (evalV_ch,evalTV_ch,evalTVs_ch); casts other thanfpextandfptruncread none; the intrinsics left byIntrinsicsOffread none (intrinsic_contractOff); calls areexec_contractOff_callwith its casesexec_contractOff_found(a defined function, or a declaration that is no intrinsic) andexec_contractOff_intrinsic; removing the flags keeps every function's name, parameters, attributes, check (Func.contractOff_check), blocks' labels, results, operands and terminators. - The step (
step_contractOff): one thread step of a thread whose frames' functions are of the fragment, under rowneverFuse oin a context ofP, is the step of the thread mapped byThread.contractOffin the context ofQwith rowo, its result mapped byStep.contractOff. It adds the block entered and itsphis (enter_contractOff, withBlock.contractOff_takeWhile), calls and returns (step_contractOff_inst,step_contractOff_term). - The grid (
Grid.lockstep_contractOff): for context families related byCtxOff(the contexts ofPwith rowsneverFuse (ω n)and those ofQwith rowsω n), the relation "the second grid is the first mapped byGrid.contractOff, and the first's frames are of the fragment (Grid.FbinOracle)" is a lockstep bisimulation (Grid.Lockstep). A thread's step is 3 (Grid.advance_contractOff), and it keeps the fragment, since a step's new frames are of module functions (step_fbinOracle,Grid.advance_fbinOracle,Grid.fire_fbinOracle). A collective call keeps the participants' positions and arguments, and resuming commutes with the map (Grid.fire_contractOff,resumeAll_contractOff,Thread.resume_contractOff). The end, the trace and the pending accesses are kept (Grid.done_contractOff,Grid.raceEnabled_contractOff,Thread.pending_contractOff). SoGrid.Lockstep.soundmovesGrid.Soundacross (Grid.Sound.contractOff), for every observation of the memory. - The start (
Grid.start_contractOff):Grid.startofQwithfn.contractOffunderωand that ofPwithfnunder the rowsneverFuse (ω n)both fail, or both succeed, the first with the second's grid mapped and its contexts with programQand rowsω n(Thread.start_contractOff). The start grid of a kernel of the fragment meetsGrid.FbinOracle(Grid.start_fbinOracle). - The start with the contexts (
Grid.start_ctxOff): 5 with the start grid ofPin the fragment (Grid.start_fbinOracle) and the two context families related byCtxOff(Grid.start_ctx): the two starts are related by the lockstep relation of 4.Except.Rel.ok_leftandExcept.Rel.ok_righttake one side's success to the other's. Kernel.Sound.contractOff: an admittedωgives the admitted rowsneverFuse (ω n)forP, whose start is sound; 6 givesQ's start andCtxOff, and 4 its soundness.- The converse (
Kernel.Sound.contractOff_iff): an oracle family of the right side isneverFuse (ω n)for anωthatadmadmits, at whichQ's start is sound; 6 givesP's start, related to it, and 4 flipped (Grid.Lockstep.symm) movesGrid.Soundback. - The observations (
Kernel.Obs.contractOff_iff): 6 relates the two starts whichever succeeds;Grid.Lockstep.reach(or its flip) takes a run of one to a run of the other ending in the related grid, which is done exactly when the first is (Grid.done_contractOff) and has the same memory.
Instances and twins
NaiveOff.correctandNaiveOff.correct_unfused(MojoProofTest/NaiveOff.lean), withmodule_eq,fn_eq,layout_eq,intrinsics_eq,naive_fbinOracle,fn_fbinOracleandbytes_neverFuse.NaiveOff.matmul_nanEqandNaiveOff.matmul_error, throughKernel.Obs.contractOff.NaiveStart.soundOff12andNaiveStart.obsOff12(MojoProofTest/NaiveStart.lean):NaiveOff.correctat the concrete launch and memory ofNaiveKernel.sound12(one block of 2 × 2 threads,m = 1,n = 2,K = 768,mem12 A Bfor everyAandB,Mat.PrebyNaiveKernel.pre12), and an observation of it at every address choice and oracle family (Kernel.Sound.exists_obs, byGrid.Sound.result).- Twin of the fragment premise: the bfloat16 naive kernel of the corpus (
ba3789d9) widens its words withfpext, whose result reads the whole oracle number, and failsProgram.fbinOracle(bf16_fbinOracle). - Twin of the converse at a fusing family (
ContractOff.twin, MojoProofTest/ContractOff.lean, a test of one example). The kernelfmaKstores%1 = fmul contract float 1 + 2^-23, 1 - 2^-23,%2 = fadd contract float %1, -1.0to a buffer of 4 bytes; its programfmaPis of the fragment andfmaQhas its module with the flags removed (both by evaluation). OnLaunch.one, at the address choice 0 and the family with every number 1 (each flaggedfaddfuses with its first operand's product), observed by the buffer's 4 bytes: a complete run offmaKobserves the binary32 word-2^-46(0xa8800000, the product and the sum rounded once);fmaK.contractOffis sound for the word+0(the product rounds to 1.0), byGrid.start_soundfrom one evaluated run, so none of its complete runs observes-2^-46, andfmaKis not sound for the same admission and relation. The family isneverFuse (ω n)for noω, so the admission ofKernel.Sound.contractOff's conclusion at this admission is empty, the contraction-off kernel is sound for it with nothing to check, and the converse in that form fails here too. At the rowsneverFuseof the family (every number 0)fmaKobserves+0and never-2^-46, byKernel.Obs.contractOff_iff. The words are evaluated through FloatLib in the kernel (rfl,decide +kernel).
Not proved
- The converse at every admissibility is false, not only unproved:
Kernel.Soundof the contraction-off kernel foradm, or forfun ch ω => adm ch fun n => neverFuse (ω n)(the conclusion ofKernel.Sound.contractOff), does not giveKernel.Soundoffnforadmwhenadmadmits a family with a fusing row. Under such a row a flaggedfaddofProunds the product and the sum once, the contraction-off program rounds each, and the run ofPis no run ofQ; the twinContractOff.twinshows both forms at one start (the second with an empty admission, since its family isneverFuse (ω n)for noω). Which programs and fusing families keep the observations (a program whose flaggedfadds have no recorded product, or whose fused and unfused results agree) is not characterized. - Programs outside the fragment: with
fpext,fptrunc,maxnum,minnumorrsqrt.approx,neverFusechanges the numbers these read, and the transport does not apply.