Docs / Proofs

Schedule independence (T2, Grid.confluent)

Lean, in MojoProof/Sem/Schedule.lean unless named: changes S1 and S2 are made; Trace.Same, Grid.Same and Thread.pending are defined (in MojoProof/Sem/SigDef.lean, since sig reaches them); Trace.Same and Grid.Same have refl, symm and trans; L1a for steps is proved (step_pending, for every context, thread and memory, and Grid.advance_pending, for every grid), and for fires (Grid.fire_trace, for every grid), and no step or fire reads the trace (exec_retrace, step_retrace, Grid.advance_retrace, Grid.fire_retrace), with its corollaries bisim (Grid.Same.step) and the fault part of bad_eqv (Grid.Same.faults); L1c for the memory (Thread.pending_shape; with C1, a running thread of a closed grid has the same pending accesses in every reachable grid, Grid.pending_reach), and W5 (Grid.reach_keeps: reachable grids keep every block; Grid.start_reach_atCalls: a waiting thread of a grid reachable from the start is at its call); L9 (racy_iff, RaceSig.lean), L11 (Trace.Same.append, Trace.Same.swap), L12 (Trace.Same.clash, Trace.Same.kept), L13 (racy_insert) and L10 (rmw_comm on values, Mem.update_comm on memory) are proved, each for every trace, list, operation and width it names. In MojoProof/Sem/Bad.lean, at the core's Bad at sig (Grid.raceEnabled_iff reads its RaceEnabled): bad_eqv is proved (Grid.Same.bad, for every launch, contexts and grids), and bad_step (Grid.Step.bad, for every launch, contexts with one layout and closed grid whose threads own their allocas and have different thread numbers, in which no collective call that completes logs a barrier of a running thread's block of threads; a fault at a collective call persists, Grid.fire_error_advance, Grid.fire_error_fire; a fault persists after an exempt update, Grid.advance_fault_atomic, by exec's check that an atomicrmw's operand is a scalar of its type; and Grid.start_reach_bad in every grid reachable from the start; the premise of one layout is needed, twin fire_fault_twin, the check, twin update_before, and the closed grid, twin closed_twin, counterexample (i)). L3 holds by construction. Grid.confluent (conclusion 1) and Grid.start_sound (conclusion 2) are proved, for every program, kernel, launch, arguments, address choice and oracle on a caller's memory whose pointer bytes name known numbers (Mem.BytesIn Mem.Known), in MojoProof/Sem/Determinism.lean, with the diamond's case of two threads with conflicting pending accesses as two grids with a race enabled (Grid.advance_raceEnabled, Bad.lean); instances and twins in MojoProofTest/Determinism.lean. Conclusion 3 is Grid.start_refute; conclusion 4 (T2′) is Grid.confluent_owned in MojoProof/Sem/ScheduleOwned.lean, by the route of item 4 below.

The semantics this proof uses: an alloca's block number is Mem.fresh (so no renaming of blocks), the start checks the caller's memory (Mem.checkClosed) and every reachable grid is closed (Grid.Closed, schedule-owned.md), lifetime.start resets the bytes of a block (L1b), and two atomicrmws are exempt from Access.conflict only if they have one commuting operation (RmwOp.commutes), pointer and size.

Scope. Every program, launch, start memory, argument values, skeleton choice and oracle. The hypotheses are that Grid.start succeeds and that the caller's memory is closed for the arguments (Mem.Closed). The start checks the fields of Mem.Closed except above (no block from next on), which no function can check, and except the pointer bytes of the blocks from next on, which above says do not exist (Mem.checkClosed; a closed memory passes, Mem.checkClosed_of_closed); it deletes no block and refuses an allocation at a number that has one (Mem.allocNext), so it keeps every block of the caller's memory (Grid.start_blocks, proved). Kernels with conflicting accesses, with atomics on one location (reduction) and with tiles that change hands at barriers (tiled_matmul) are all covered: a schedule dependence shows up as a bad grid. T2 says that the schedule does not matter up to bad grids; it does not say that a kernel is race free. That is a property of one run, which T2 transfers to all runs.

Design choices.

Changes it needs first

Definitions the proof adds

Statement

Data. hs : Grid.start prog fn L args m ch oracle = .ok (g₀, c). The states are γ := { g // ReflTransGen (Grid.Step L c) g₀ g }, the relation is rc x y := Grid.Step L c x.1 y.1.

Conclusions.

  1. Grid.confluent hs : Confluent rc (fun x y => Grid.Same x.1 y.1) (fun x => GPUProof.Bad sig (x.1, L, c)). (Proved, with the caller's hypothesis hm : m.BytesIn m.Known; instance confluent2.)
  2. Through Confluent.one_run and Grid.sound_of_one_run: if one run from g₀ reaches a done grid z with a race-free trace that keeps !invariant.load (a done grid does not fault, done_not_faults, and has no running thread and no fire that completes, so no enabled race), then Grid.Sound L c g₀ obs R for every obs that reads only Mem.blocks, with R the property of obs z.mem. This is a statement about every schedule from one run; whether the run exists is a fact about one kernel and one input. (Lean: Grid.start_sound, taking the run as a Path with ¬ GPUProof.Bad sig (z, L, c); instances sound2 and final2 on start2, two threads' alloca and store; twins racy_twin, a done grid with a racy trace, so sound for no observation, and trace_twin, an observation of the trace's newest event, for which two schedules differ, so sound for no relation.)
  3. Refutation transfer, the same theorem read backwards: if some reachable grid is bad (a fault, a race or a broken promise in one schedule), then every reachable grid without a step is bad. No schedule hides it. (Lean: Grid.start_refute by Confluent.bad_normal; instance refuteP on startP, twin refute_twin: on start2, done2 has no step and is not bad.)
  4. T2′ (schedule-owned.md) becomes a corollary. Under hH, hX and hI, no reachable grid has a racy trace, a broken promise or an enabled race (schedule-owned.md, conclusion 2, applied to the trace with the pending accesses in front), so on the states γ Bad is Faults, and Confluent with Bad := Faults follows by a general lemma: Confluent r e B and ∀ a, B a ↔ B' a give Confluent r e B'. (Lean: Confluent.transfer along the identity, GPUProof/Transfer.lean; Grid.owned_bad, Grid.confluent_owned, in MojoProof/Sem/ScheduleOwned.lean; hX there is own injective on the start grid's thread numbers, and the caller's memory meets T2's Mem.BytesIn Mem.Known.)

Lemmas used

From schedule-owned.md, unchanged: L1a (steps and fires do not read the trace; a successful step logs exactly its pending accesses, proved as step_pending and, for the grid, Grid.advance_pending; a barrier fire logs one .barrier, proved as Grid.fire_trace), L1b (what a step changes, including the bytes of the thread's own blocks that lifetime.start makes undefined; done for every step in every grid reachable from the start, Grid.start_reach_changes, MojoProof/Sem/Writes.lean), L2 (frame; done for statements and steps, exec_agree, step_agree and step_fault, and the memory part of the no-conflict argument below, Mem.Changes.agree, MojoProof/Sem/Frame.lean; that changes at disjoint places commute, Mem.run_comm, and the case of two threads' steps of the diamond if no pending access of either conflicts with one of the other (Access.conflict), Grid.advance_comm, which covers both of its subcases below (no write of one overlaps an access of the other; or an exempt pair of atomicrmws, Grid.advance_atomic_comm), and in every grid reachable from the start for every two different threads, Grid.start_reach_comm; the premise of the first subcase from ownership, if each of the two threads' pending accesses follows a discipline with different tiles or is to a block its thread owns, Grid.pending_apart, so from the start Grid.start_reach_comm_of_owned; its case of a thread's step and a collective call, a barrier or a shuffle, for every grid, if the thread's context is not of the barrier's block of threads, Grid.advance_fire_comm, and in every grid reachable from the start, Grid.start_reach_fire_comm; its case of two collective calls, for every grid and contexts in which the two threads have one layout, Grid.fire_fire_comm, so for every grid in the start's contexts, Grid.start_fire_fire_comm, MojoProof/Sem/Confluence.lean), L3 (choices), W2, W5, and Grid.Closed with C1 and C2 (which hold of every reachable grid; their proof does not use ownership). New:

Proof

refl, symm, trans: equality and List.Perm on (phases tr).1. (Lean: Trace.Same.refl, symm, trans and Grid.Same.refl, symm, trans.)

bisim. Take Same a a' and a step a → b. By L1a the same thread (or the same fire) steps from a', since its outcome reads the threads and the memory without the trace, which Same keeps. It logs the same events E and makes the same changes. So b' has the threads, blocks and next of b, and the traces are E ++ tr and E ++ tr', which are Trace.Same by L11. (Lean: Grid.Same.step, for every launch and context map; a grid the same as a is a with its trace replaced, Grid.Same.eq_retrace.)

bad_eqv. Faults reads the threads and the memory without its trace (L1a; done: Grid.Same.faults). racy and the core's kept at sig are invariant by L12. For RaceEnabled, the pending accesses are the same in a and a' (same threads, same blocks), and Trace.Same (P ++ tr) (P ++ tr') by L11, so L12 applies. (Lean: Grid.Same.raceEnabled, with Thread.pending_retrace: the pending accesses read no trace; Grid.Same.bad, for every launch, contexts and grids; instance MojoProofTest/Bad.lean same_bad.)

diamond. Take g → b and g → c with g reachable.

bad_step. Let g be bad and g → g'.

W3 and W4, and the invariants on vadd

The proof does not use W3 or W4. W1 to W5 hold at every grid reachable from the grid Grid.start returns, for every program, and are checked on vadd on a launch of 2 blocks of 2 threads.

Dependencies

Instances and twins