Grid.Sound from one run (Grid.sound_of_one_run)
Lean: MojoProof/Sem/Trace.lean.
Scope. Every program, launch L, contexts c, observation obs and relation R. Confluence is a
hypothesis; this theorem does not prove it for any kernel.
Statement
The data are a state type γ, a projection π : γ → Grid, relations rc and e on γ, and a set
Bad ⊆ γ. The hypotheses:
hc : Confluent rc e Bad(one-run.md);proj:rc x x'givesGrid.Step L c (π x) (π x');lift:Grid.Step L c (π x) g'givesx'withrc x x'andπ x' = g';hF,hT,hK: ifπ xfaults, its trace isracy, or the core'skeptatsigof its trace's core events (GPUProof.kept sig (((π x).mem.trace.map (Event.toCore L)).reverse)) is false, thenBad x;hfin: ifGPUProof.Done sig (π x, L, c)(every thread of the launch has returned),π xhas noGrid.Step;hD:e x x'andGPUProof.Done sig (π x, L, c)giveGPUProof.Done sig (π x', L, c);hO:e x x'givesobs (π x).mem = obs (π x').mem;run : Path rc n x₀ z,hz : GPUProof.Done sig (π z, L, c),hb : ¬ Bad z,hy : obs (π z).mem = some y,hR : R y.
Conclusion: Grid.Sound L c (π x₀) obs R, all seven fields (safe, raceFree, kept, progress,
terminates, determinate, post).
Proof
The one-run argument is GPUProof.Clean.of_lift's (GPUProof/Sound.lean, docs/proofs/theorem0.md),
for every machine; this theorem is its case at sig along x ↦ (π x, L, c), with the observation
fun s => obs s.1.mem of the grid's whole memory. A step at sig from (g, L, c) is a grid step
with (L, c) kept (sig_step_iff), so proj, lift and hfin give the core's projection, lift
and hfin; hF, hT, hK, hD and hO are already stated at sig.
Clean.of_lift's argument.
zis normal forrc: a steprc z x'projects to a step from the doneπ z, whichhfinexcludes.- Lifting reachability (
ReflTransGen.of_lift): every state that the core's steps reach fromπ x₀isπ xfor somexthatrcreaches fromx₀, by induction, usingliftat each step. Confluent.one_runwithrun, the normality ofzandhbgives four facts:rcis well founded fromx₀; noxreachable fromx₀is bad; every normalxreachable fromx₀ise-equivalent toz; every reachablexreaches somez'equivalent toz.Clean's fields: a fault, a racy trace or a broken promise at a reachableπ xwould makexbad (hF,hT,hK); ifπ xis not done, the run fromxto az'equivalent tozis not empty (elsehDwithhzmakesπ xdone), and its first step projects; termination isAcccarried alongπ(Acc.of_lift).- A reachable done state is
π xwithxnormal, soe z x, andhOgives it the observation ofπ z.
Back to the grid (Grid.Sound.of_clean, MojoProof/Sem/Spec.lean). Every grid that π x₀
reaches gives a state that (π x₀, L, c) reaches (sig_reach_iff), so safe, raceFree and
kept are Clean's; a grid that is not done has a step because its state has one
(sig_step_iff); terminates is Clean's, along g ↦ (g, L, c). determinate and post: a
reachable done grid observes obs (π z).mem = some y, and R y.
Dependencies
Confluent,Path,Confluent.one_run(GPUProof/OneRun.lean);GPUProof.Clean.of_lift(GPUProof/Sound.lean);Grid.Step,Grid.faults_iff,Grid.Sound,racy(MojoProof/Sem/Grid.lean,MojoProof/Sem/Spec.lean);sig_step_iff,sig_reach_iffandGrid.Sound.of_clean(MojoProof/Sem/Spec.lean);GPUProof.FaultsandGPUProof.Done(GPUProof/Run.lean) andGPUProof.kept(GPUProof/Race.lean) atsig;Relation.ReflTransGen(Mathlib).
Instances in the tree
Grid.start_sound(MojoProof/Sem/Determinism.lean): a launch's start grid.γis the subtype of grids reachable from the start grid,rcisGrid.Stepon it,eisGrid.Same, andBadis a fault, a racy trace or a broken!invariant.loadpromise: the case whereγis the reachable grids.Grid.sound_commuting:Nthreads that each return in one step with a change of memoryf n, for everyN, launch of at leastNthreads, contexts, threads and memoryM, when, at every memory with a propertyPthatMhas and everyf nkeeps, the step of threadn < Nreturns with memoryf n m, the changes of two distinct threads commute and each keeps the trace; the observation is that of(List.range N).foldr f M, andM's trace has no race and keeps!invariant.load.γis the set of returned threads, the memory of a set is that of its threads' changes,rcreturns one thread,eis=, andBadis empty. Its instance with changes isMojoProofTest.Close.sound_dead: 2 and 3 threads, each ending its ownallocablock (Mem.setLive), on one memory of 3 live blocks.Grid.sound_returning: its case ofNthreads that each return in one step without changing memory (f n m = m,Pis· = M), for everyN.γis the set of returned threads,rcreturns one thread,eis=, andBadis empty. The diamond is proved for every pair of threads. Its instance at one thread isMojoProofTest.Close.sound_one_ret: one thread ofret voidon the empty memory atLaunch.one, its premises bydecide.Kernel.sound_ret_launch: the kernelret voidon every launch and every closed memory (Mem.Closed, which the start's check needs), throughGrid.sound_returning.
The observation of an elementwise output
Lean: MojoProof/Sem/Elementwise.lean; proved. Instances and twins: MojoProofTest/Elementwise.lean.
This is the obs side of Grid.sound_of_one_run for a kernel that
stores one element per cell: what Mem.tensor (MojoProof/Sem/Buffer.lean) reads after the stores.
Statements. For a layout l, type t, codec enc for t (Layout.Codec), shape s, offsets
off that are l.stored t bytes apart (Apart), a tensor x, values f i with encodings bs i
(l.encode t (enc (f i)) = some (bs i) for every cell i) and a list of cells is:
Block.holds_writes: if blockblkholdsx(Block.Holds), then writingbs iatoff ifor the cells ofis, in the order ofis(repetitions allowed), gives a block that holds the tensorywithy i = f ifori ∈ isandy i = x iotherwise.Mem.tensor_writes: a memory whose blockbis that block observessome yatb.Mem.tensor_vec: the case of shape[N], binary32 (.fp .float,Val.ofFloat .float) at row-major offsets frombase, andisthe cells belown(i ∈ is ↔ i < n): the observation isi ↦ w ibelownandx ifromnon.Mem.tensor_vec_add: in 3 with every cell stored (n = N) andw i = deliver (a i + c i) (k i), which is whatfaddwith no flags gives on the words ofa iandc ifor the oracle number3 k i(floatBin_ofFloat), and noa i + c ia NaN: the observation issome (a + c), where+on tensors is TorchLean'sAdd (Tensor α s)(zipWith (· + ·)) and on elements FloatLib's binary32 addition, the instanceAdd (Model fmt). No copy of either is defined.
Proofs.
- Writes change no block's size (
Block.size_puts, by induction on the list), so each tile still lies in the block. The encodings havel.stored tbytes (the codec's round trip), and the tiles are apart, so after the writes thel.stored tbytes atoff iarebs iifi ∈ isand the block's own otherwise (Block.read_puts). In the first case they decode toenc (f i)(the codec's round trip), in the second toenc (x i)(blkholdsx). Both areenc (y i)(Tensor.Rep.get_ofFn). - Through a codec a block holds at most one tensor, so the observation is
y(Mem.tensor_eq). - Binary32 words are a codec (
Layout.Codec.float) and row-major offsets are apart (apart_rowMajor); rewritei ∈ isbyi < n. - By 3, the observation is
i ↦ deliver (a i + c i) (k i)(every cell is belowN). With no NaN,deliver r n = r(deliver_of_isNaN_eq_false), and(a + c) i = a i + c i(Tensor.add_apply), so the two tensors agree at every cell (Tensor.Rep.ext).
Instances and twins. A 16-byte block holding the zero tensor of shape [4], row-major from 0:
- after one store per cell below 2 (order 1, 0), the observation is the stored words at cells 0
and 1 and zero at 2 and 3 (
vec_inst); - with
a= 1.0 andc= 2.0 in every cell, the four cells stored in the order 3, 1, 0, 2 withdeliver (a i + c i) 0(fadd_word: whatfaddgives for oracle number 0), the observation isa + c(add_inst), 3.0 in every cell (sum_three); - twin of the coverage premise: with cells 0 and 1 stored, the observation is not
a + c, cell 3 is still 0 (add_twin_partial); - twin of the NaN premise:
+∞ + -∞is a NaN, and the oracle number of the NaN0x7fc00001stores another NaN, so the observation is notpinf + ninfthough every cell is stored (add_twin_nan). Without the NaN premise the observation is related toa + conly up to NaN words.
Scope. These are statements about the bytes of a block after writes, for every layout, shape
and order of writes. That the stores of a kernel run write exactly these bytes, one per cell, is
the per-thread part of a kernel theorem (for vector_addition, with the index of
docs/proofs/index.md); it is not proved here.