Docs / Proofs

The kernel theorem from one run per address choice (Kernel.skel_sound, Kernel.Sound.of_blocks)

Lean: MojoProof/Sem/SkelStep.lean, section "The kernel theorem". Instances and twins: MojoProofTest/SkelKernel.lean, MojoProofTest/SkelVadd.lean and MojoProofTest/SkelVadd23.lean. The statements hold for every program, kernel, launch, arguments, labelling, payload blocks, memories and set of address choices that meet their premises.

Where it sits

Grid.skel_six (payload.md, "The six-field lemma") turns one run on a first memory m₁ at one address choice into the six fields of Grid.Sound, with the blocks as observation, for every memory m₂ related to m₁ by Mem.SkelEq pay and every oracle. Its proof is one use of Grid.sound_of_one_run (adapter.md), through Grid.skel_run. Kernel.Sound (MojoProof/Sem/Spec.lean) asks for a start and Grid.Sound at every admitted pair of an address choice and an oracle. Grid.Sound speaks of every grid reachable from the start, so of every schedule: every order in which Grid.Step advances threads and fires collective calls. The two theorems below state what Grid.skel_six gives at the level of Kernel.Sound, and how the seventh field is added there.

Statements

  1. Kernel.skel_sound. Premises: Labels.Ok prog lab = true; fn is the module's function of its name; m₁.BytesIn m₁.Known (the pointer bytes of the first memory name blocks it has); a set A of address choices such that for every ch in A there are an oracle o₁, a start Grid.start prog fn L args m₁ ch o₁ = .ok (g₁, c₁) with lab.MemOk pay L c₁ g₁, and one run ReflTransGen (Grid.Step L c₁) g₁ z₁ to a grid z₁ in which every thread has returned (GPUProof.Done sig (z₁, L, c₁)) and that is not bad (¬ GPUProof.Bad sig (z₁, L, c₁): no thread faults, no race is enabled, its trace has no data race and keeps the promise of !invariant.load); and m₁.SkelEq pay m₂. Conclusion: Kernel.Sound prog fn L args m₂ (fun ch _ => A ch) (fun m => some m.blocks) (fun _ => True): at every address choice in A and every oracle, the launch on m₂ starts, and its grid is safe, race free, keeps the promise of !invariant.load, makes progress, terminates and is determinate for the blocks, under every schedule. With A = (· = ch) this is one run at one address choice; o₁ may depend on ch.
  2. Kernel.Sound.of_blocks. For every program, kernel, launch, arguments, memory m, admission adm, observation obs that reads only the blocks (m.blocks = m'.blocks → obs m = obs m') and relation R: if the kernel is sound for adm, the blocks and fun _ => True, and for every admitted (ch, o) the start's grid reaches one grid in which every thread has returned and whose observation is some y with R y, then Kernel.Sound prog fn L args m adm obs R.

Proofs

  1. For an admitted ch and any oracle o₂, take o₁, the start, Labels.MemOk and the run that the premise gives at ch, and apply Grid.skel_six at m₂ and o₂.
  2. For an admitted (ch, o), the start and Grid.Sound for the blocks come from the first premise, one final grid from the second, and Grid.Sound.of_blocks gives Grid.Sound for obs and R: every final grid has the blocks of the given one (determinate), so the same observation.

Scope

Labels.MemOk at more threads: counts and the route

memCheck closes Labels.MemOk with every, which checks one grid per schedule prefix. Counted on vector_addition from the start of MojoProofTest/SkelVadd.lean (blocks 1, 2, 3, count 5, mem5, ch₀, ω₀), by a breadth-first search over succs evaluated with #eval (not in the repository) that merges grids agreeing on every thread's status, step count and frames (label, statement number, registers 0 to 63), the memory's next block number and bytes 0 to 23 of blocks 1 to 3, and counts the schedule prefixes with multiplicities:

The search's peak resident memory was 4.8 GB. At every launch measured, the merged grids are exactly the vectors of per-thread step counts, ∏ (nᵢ + 1): the threads write disjoint cells, so how far each thread has run fixes every part of the grid that the merging compares, and no merging that tells the threads' step counts apart does better. Merging also needs a proof that grids it merges have the same successors up to merging and the same Labels.MemAll, which every_sound does not give.

So neither every nor a merging every closes Labels.MemOk at L23 by evaluation. The route is a thread-modular lemma: Labels.MemAll asks, of each running thread, only Labels.MemAt at its own next statement in its own scope (Labels.MemNext). For a kernel without barriers or shuffles whose skeleton loads read only bytes that no thread writes (for vector_addition, the byval argument structs), each running thread of a reachable grid has the frames of a grid on its own run with the other threads not moving, so Labels.MemOk.of_closed, with the set of grids in which every running thread is at a point of its own run, gives Labels.MemOk from one run of each thread (6 runs of at most 29 grids at L23). The proof obligation is that set's closure under a step of another thread, which changes only that thread's frames and the bytes of its stores.

The lemma is Labels.MemOk.of_threads (MojoProof/Sem/Labels.lean), for every labelling, payload blocks, launch, contexts and start grid. Its premises are a property I of memories and a property R n s of thread n's status s that no waiting status has, both true at the start grid; a step of a running thread n from a grid whose memory has I and whose statuses have R gives a grid whose memory has I and in which n's status has R; and each running thread of such a grid meets Labels.MemNext in that grid's memory. The proof is Labels.MemOk.of_closed on the grids whose memory has I and whose statuses have R: a step of n sets only n's status (Grid.advance_set), and no collective call completes, as a call completes only at a waiting thread (Grid.fire_trace). Its instance, store3_ok (MojoProofTest/Labels.lean): three threads that each store the payload register to block 1 meet Labels.MemOk with block 1 payload, with I true of every memory and R n the three statuses of thread n on its own; with no block payload they do not.

At L23 the lemma needs R n to be "a status of thread n's solo run, or returned", which no waiting status has as vector_addition has no barrier or shuffle, and I to be "the bytes that the skeleton loads read, the byval argument structs, are as at the start, and every block has its start shape". The step premise is then step_agree (MojoProof/Sem/Frame.lean) from the memory of the solo run to the memory reached: they agree at the pending reads of n, so n's step gives the next status of its solo run, and its store writes only the output buffer, which keeps I. Each of the six solo runs has at most 29 statuses, each evaluated once.

This is in Lean, MojoProofTest/SkelVadd23.lean, with a stronger I: EqOff 3 · m₀, the memory has the start memory m₀'s next block number and, at every number, no block or a block of the same shape, read-only flag, owner and liveness as m₀'s, with the same bytes below its size except in block 3, the output buffer. R n s (RefStatus) is: returned, or running a thread that passes walk on m₀, the Boolean check of thread n's run alone with every step from m₀ itself. At each step the check asks that the pending reads miss block 3 and stay below their blocks' sizes (readsOk), that Labels.MemNext holds (memNextB of MojoProofTest/SkelVadd.lean), and that the step changes nothing but block 3's bytes and adds no block at its fresh number (stepOk); the run never waits. The kernel evaluates the six checks at the start (check23_ok). The step premise is Grid.advance_agree (MojoProof/Sem/Confluence.lean): EqOff gives Mem.Agree at the pending reads (EqOff.agree, with Thread.pending_shape for the pending reads at the reached memory), so the step from the reached grid gives the status and the memory changes of the step on m₀, and the same changes keep EqOff (EqOff.run, run_none for the fresh numbers). Labels.MemNext moves from m₀ to the reached memory by the blocks' shapes (memAt_shape). This gives memOk23; with the run to the end by Grid.run evaluated by the kernel (runCheck23_ok), Kernel.skel_sound gives skel_vadd23: for every memory related to mem5 by Mem.SkelEq, vector_addition at L23 is sound at address choice ch₀ and every oracle, for the blocks and the trivial relation. The module builds in 5 to 10 s, at 4.2 GB peak resident memory.