Docs / Proofs

Schedule independence by ownership (T2′, Grid.confluent_owned)

Lean: conclusions 1 and 2 proved, for every program, kernel, launch, arguments, address choice, oracle and discipline, in MojoProof/Sem/ScheduleOwned.lean (Grid.confluent_owned, Grid.owned_clean, Grid.owned_race, Grid.owned_bad), by the route of section "Route in Lean" below: T2 (Grid.confluent, schedule.md) with its bad set replaced by the faults (GPUProof.Faults at sig) (Confluent.transfer along the identity), which conclusion 2 allows. Conclusion 3 is proved in a stronger form, for every program, kernel, launch, arguments, address choice, oracle and discipline (Grid.owned_sound, with the trace lemma racy_kept_of_local and Grid.Done.bad_iff, MojoProof/Sem/Determinism.lean): its premises are on the trace of the done grid of the one run, not on every reachable grid (section "Conclusion 3 in Lean"). This document's own route, below the statement, is not formalized; it is kept because it needs no commutation of atomics.

It uses that an alloca's block number is Mem.fresh, from its thread and step count, and that no step changes Mem.next. That an alloca reads its addresses from the skeleton choices (Ctx.addr) and that the oracle holds the float choices only are not needed: this proof fixes both. The lemmas it uses (section "Lemmas used") are proved, for the Lean route, in schedule.md's setting: L1a (step_pending, Grid.advance_pending, Grid.fire_trace, and exec_retrace, step_retrace, Grid.advance_retrace, Grid.fire_retrace for the trace no step or fire reads), MojoProof/Sem/Schedule.lean, and with it bisim and the fault part of bad_eqv (Grid.Same.step, Grid.Same.faults); L1c for the memory (Thread.pending_shape) and W5's block part (Grid.reach_keeps: every grid reachable from g keeps every block of g, and, C1's part about memory, its next and every number below next with no block free); C1's other part, the closed-grid invariant (Grid.reach_closed and Grid.pending_reach, MojoProof/Sem/Closed.lean: every step and fire keeps Grid.Closed, and a running thread of a closed grid has the same pending accesses in every grid reachable from it; from the start, on a caller's memory whose pointer bytes name known numbers, Grid.start_closed, Grid.start_reach_closed and Grid.start_pending); W5's other part (Grid.start_reach_atCalls: in every grid reachable from the start, every waiting thread is at its collective call, Thread.AtCall); and L1b (step_changes, and in every grid reachable from the start Grid.start_reach_changes, MojoProof/Sem/Writes.lean; Grid.fire_blocks, Schedule.lean). MojoProofTest/Launch.lean checks the start's invariants on a launch that allocates, stores, waits at a barrier and returns.

The start makes the invariants hold. Grid.start first checks the caller's memory (Mem.checkClosed, MojoProof/Sem/Grid.lean): a memory with next = 0 or a block 0 is unsupported, and so is one in which an argument, or a pointer byte of a block below next, names a number from next on. The launch's own blocks (shared memory, byval copies) come at next and above. An alloca whose number is in use is unsupported, and so is an allocation of the start (Mem.allocNext) at a number that has a block, so no block is ever overwritten, and the start deletes no block of the caller's memory (Grid.start_blocks, proved for every program, launch, memory and choice). Mem.checkClosed checks the fields of Mem.Closed except above (no block from next on), which no function can check, and except the pointer bytes of blocks from next on, which above says do not exist; so T2 takes the hypothesis that the caller's memory is closed for the arguments. The invariant that every block number a thread holds is allocated (W1) is replaced by the closed-grid invariant Grid.Closed, which holds at the start of a closed memory and which every step keeps (section "The closed-grid invariant"). Without the check the statement is false: a pointer argument naming the number that another thread's alloca takes later faults in one order and runs in the other (the counterexample in section "The closed-grid invariant"). lifetime.start makes the bytes of a thread's own block undefined; L1b, L2, the disjointness argument and bad_step include those bytes.

Scope. Every program, launch, start memory, skeleton choice and oracle, under the hypotheses below. The hypotheses hold for kernels whose memory accesses follow one tiling for the whole run (vector_addition). They fail for kernels whose shared-memory tiles change hands at a barrier (tiled_matmul, which needs race freedom per barrier interval, barrier-owned.md) and for kernels with atomics on one location (reduction, which needs L10: two atomicrmws that Access.conflict exempts, of one commuting operation on the same bytes, commute). The hypotheses constrain where accesses go, not whether steps fault. Faults are the bad states. An out-of-bounds access lies in no tile, so a kernel with one is not covered.

Statement

Data. hs : Grid.start prog fn L args m ch oracle = .ok (g₀, c) and hm : m.Closed args, for every start memory m and argument values args. A closed memory passes the start's check (Mem.checkClosed_of_closed), and the start keeps every block of m (Grid.start_blocks). Let N := g₀.threads.size, the number of the start grid's threads. The Lean statement takes T2's hypothesis on the caller's memory instead of hm: every pointer byte of its blocks names a number below next or with a block (hm : m.BytesIn m.Known, schedule.md). 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. A discipline is a tiling map T : Nat → Option (Tiling ι) and a tile map own : Nat → ι (Tiling, Owned: MojoProof/Sem/Trace.lean).

A definition the proof adds (part of L1). Thread.pending (c : Ctx) (t : Thread) (m : Mem) : List Event lists the accesses that the step of t from m makes: for a load, store or atomicrmw at the thread's position whose pointer operand evaluates, its access (two for atomicrmw, read then write), and nothing otherwise. This holds whether the step then succeeds or faults. On success the step logs exactly these accesses (step_pending). (Proved in MojoProof/Sem/Schedule.lean, as a list of events, newest first, so an atomicrmw's write is in front of its read; step_pending for every context, thread and memory.)

Hypotheses.

Equivalence. Grid.Same g g': g.threads = g'.threads, g.mem.blocks = g'.mem.blocks, g.mem.next = g'.mem.next, and g.mem.trace is a permutation of g'.mem.trace (List.Perm). No renaming of blocks is needed: a block's number (Mem.fresh) does not depend on the schedule. T2 (schedule.md) replaces List.Perm with Trace.Same, which also keeps each access's barrier phase, and proves T2′ as its corollary; the Lean proof follows schedule.md, and this document's own route is kept as the one that needs no atomic commutation.

Conclusions.

  1. Confluent rc (fun x y => Grid.Same x.1 y.1) (fun x => GPUProof.Faults sig (x.1, L, c)).
  2. For every reachable g: racy g.mem.trace = false and GPUProof.kept sig ((g.mem.trace.map (Event.toCore L)).reverse) = true; and if a race is enabled in g (the core's RaceEnabled at sig), g faults (the racing thread's step is a fault; a fire that completes at a reachable grid adds no race, since the trace is not racy). So on reachable grids the core's Bad at sig is GPUProof.Faults at sig: no reachable grid has a racy trace or a broken promise of !invariant.load. An enabled race is not excluded, only one whose step succeeds: Stmt.pending lists an access before the step checks it, so a thread's pending access may conflict with another's and still be one the memory refuses.
  3. Corollary through Grid.sound_of_one_run (adapter.md): if one run reaches a done grid z and obs depends only on Mem.blocks, then Grid.Sound L c g₀ obs R with R the property of obs z.mem. hT and hK come from conclusion 2. hF holds because Bad is Faults. hD and hO hold because Same keeps the threads and the blocks. The Lean form (Grid.owned_sound) goes through T2's Grid.start_sound instead, and needs the discipline of the done grid's trace only: section "Conclusion 3 in Lean".

Why the bad set is Faults. Bad := False would make the adapter's hF say that no reachable grid faults, which the hypotheses do not give: a kernel can fault in every schedule. Bad := Faults needs bad_step, which says that faults persist. Under hH this is a frame argument (case P below). It needs neither the core's RaceEnabled nor L7.

Lemmas used

The closed-grid invariant (W1)

A grid holds a pointer if it occurs (inside vectors and aggregates too) in a register, the fmul operands (prods) or the allocas of a frame of a thread, in the arguments of a waiting thread, in the globals table of a context, or as a pointer byte .ptr p i below the size of a block. Define

Grid.Closed g := ∀ p held by g, p.block < g.mem.next ∨ (g.mem.blocks p.block).isSome.

In Lean (MojoProof/Sem/Names.lean and Closed.lean; Mem.Known in SigDef.lean), a number is known to m (Mem.Known) if it is below m.next or has a block, and Grid.Closed c g has four fields: null (0 is known), threads (Status.NamesIn: every value in a register of a frame of a thread, and every argument of a waiting thread, names only known numbers, Val.NamesIn), bytes (Mem.BytesIn: every pointer byte below the size of a block does) and globals (every global of every context c n does). It leaves out prods and allocas, as no step takes a pointer from them: an fadd makes only floats from the fmul operands, and a return's setLive on the allocas changes only liveness. Proved, for every program, context map, launch and grid: Step (exec_closed, enter_closed, step_closed, Grid.advance_closed, Grid.fire_closed; every grid reachable from a closed grid is closed, Grid.reach_closed) and C1 below (Grid.pending_reach, from Grid.reach_keeps, Mem.Keeps.sameShape and Thread.pending_shape) and Start (Grid.start_closed, for every program, kernel, launch, arguments, choices and memory whose pointer bytes name known numbers, Mem.BytesIn m m.Known, which a closed memory has, Mem.Closed.bytesIn; the start's check gives the rest, Mem.checkClosed_ok). So C1 holds from the start (Grid.start_reach_closed, Grid.start_pending). The premise on the bytes is needed: the memory mem100 (next = 2, one block 5 holding a pointer to block 100) passes the check and starts, and its grid is not closed (MojoProofTest/Closed.lean, start_twin). Not proved: C2.

Two consequences carry the proof.

Under Closed, an alloca's number is never in use in a reachable grid. The blocks at numbers from next on are those of earlier allocas, m.fresh n' k' with k' below thread n''s count. The check in exec is kept anyway, because with it L1b needs no invariant.

Counterexample without the start's check (MojoProofTest/Eval.lean, startMemTests). Two threads run %a = alloca i32; %x = ptrtoint ptr %p to i64; ret void with %p = .ptr ⟨5, 0⟩. Without the check, next = 3 after the buffer (block 1) and the dynamic shared memory (block 2), and thread 1's alloca takes 3 + Nat.pair 1 0 = 5. Thread 0 first: its ptrtoint is undefined behavior (pointer to no block). Thread 1 first: block 5 exists, and both threads run to the end. A step takes a faulting grid to one that does not fault, against bad_step. With the check, the caller's memory (the buffer as block 1, next = 2) names block 5 from next on through the argument, so the start (Mem.checkClosed) refuses the launch, in both orders; the test checks the refusal.

Proof

Disjointness. Let two different threads n and m both run in a reachable g. Each write that n's step makes lies in tile own n of a tiled block, or in a block owned by .thread n. Owned.read forbids writes to untiled blocks. Each read that m's step makes lies in tile own m, in an untiled block, or in a block owned by .thread m. Tiles own n and own m are disjoint (hX, Tiling.disj). Untiled blocks are never written, and owners differ. So n's writes miss m's reads and m's writes. Both steps add blocks only at numbers keyed by their own thread, which the other thread does not name (C2), and change liveness, and reset bytes (lifetime.start), only of their own blocks, to which the other thread's accesses fault in every state (Mem.check).

In Lean, the part about memories (MojoProof/Sem/Frame.lean, Mem.Changes.agree): if m' is m changed as L1b allows (Mem.Changes L N m m', with events d in front of its trace), then m and m' agree for thread w at S and the reads of P (L2's Mem.Agree) if N has no number of S, no block of m at a number of S and L admits w, and no access of d writes a byte that a read of P reads in a block of S that admits w:

theorem Mem.Changes.agree {L N : Nat → Prop} {m m' : Mem} {w : Who} {P : List Event}
    (h : m.Changes L N m') (hN : ∀ b, S b → ¬ N b)
    (hL : ∀ b x, S b → L b → m.blocks b = some x → x.owner.admits w = false)
    (hd : ∀ d, m'.trace = d ++ m.trace → ∀ b x i a, S b → m.blocks b = some x →
      x.owner.admits w = true → Reads P b i → .access a ∈ d → ¬ a.Writes b i) :
    m.Agree w S P m'

With m' the memory after m's step (Grid.start_reach_changes: L the blocks m owns, N its fresh block, d its pending accesses), S n's names and P n's pending accesses, the three premises are C2, the owners and the tiles; that they hold in every reachable grid is not yet proved. Instance: MojoProofTest/Frame.lean, changes_agree (a store of bytes 0 to 3 and a read of bytes 4 to 7); the twin changes_twin: at a read of the bytes the store writes, the memories do not agree.

refl, symm, trans hold because Same is equality and List.Perm.

bisim. Take Same a a' and a step a → b. By L1a the same thread (or the same fire) steps from a' to b', with the same new events in front of a permuted trace. So Same b b'. (Lean: Grid.Same.step, for every launch and context map.)

bad_eqv. Faults reads the threads and, through step and Grid.fire, the memory without its trace (L1a). Same keeps both. (Proved for Faults: Grid.Same.faults.)

diamond. Take two steps g → b and g → b', with g reachable.

The two-thread case in Lean (done, under its premises). step_agree gave ∃ ops without saying where the ops act, and two lists of ops commute only if they act at different places. Five steps, all done:

  1. Mem.Op.At m w L N W o (MojoProof/Sem/Frame.lean): where op o of a step of thread w on memory m acts. A put .put b o bs is into a block of m whose owner admits w, and every byte i from o below o + bs.length is written by an access of W (Access.Writes a b i); a set .set b _ has N b and no block at b in m; a liveness change .live b _ has L b; a log .log e has e an access (a step logs its pending accesses, step_pending, and Stmt.pending lists only accesses). The puts are the stores' Mem.writes, the sets the allocas at the fresh number, the liveness changes lifetime.start and .end (Intrinsic.ops, intrinsic_at) and the returns' allocas.

    def Mem.Op.At (m : Mem) (w : Who) (L N : Nat → Prop) (W : List Event) : Mem.Op → Prop
    
      | .put b o bs => (∃ x, m.blocks b = some x ∧ x.owner.admits w = true) ∧
          ∀ i, o ≤ i → i < o + bs.length → ∃ a, .access a ∈ W ∧ a.Writes b i
    
      | .set b _ => N b ∧ m.blocks b = none
      | .live b _ => L b
      | .log e => ∃ a, e = .access a
    
  2. exec_agree and step_agree give the places of their ops, with W the events the ops log: step_agree's conclusion is

    ∃ ops, (∀ o ∈ ops, o.At m₁ c.who (fun b => m₁.Owned c.who.thread b ∨ ∃ f ∈ t.frames,
        b ∈ f.allocas) (· = m₁.fresh c.who.thread t.k) (ops.filterMap Mem.Op.event)) ∧
      r = r.withMem (m₁.run ops) ∧ step c t m₂ = .ok (r.withMem (m₂.run ops))
    

    and exec_agree's the same with L the blocks of s.mem the thread owns and N the fresh number at step k. Mem.write_agree and Mem.store_agree give a store's put inside the block, in a block that admits the thread.

  3. Mem.run_comm: ops at disjoint places commute. Proved (MojoProof/Sem/Confluence.lean, for every memory and all ops; Mem.Op.Apart and, for each number c, Mem.Op.on c, what an op does to the block there). Two puts at one block are apart if no byte the first puts is one the second puts (∀ i, o ≤ i → i < o + bs.length → i < o' ∨ o' + bs'.length ≤ i), so empty puts are apart from every put:

    theorem Mem.run_comm (m : Mem) {os₁ os₂ : List Mem.Op}
        (h : ∀ o₁ ∈ os₁, ∀ o₂ ∈ os₂, o₁.Apart o₂) :
        ((m.run os₁).run os₂).blocks = ((m.run os₂).run os₁).blocks ∧
          ((m.run os₁).run os₂).next = ((m.run os₂).run os₁).next ∧
          Trace.Same ((m.run os₁).run os₂).trace ((m.run os₂).run os₁).trace
    

    Instances: MojoProofTest/Confluence.lean comm and same (puts at the two halves of one block, writes by threads 0 and 1 logged, and a set); the twins twin (two puts at byte 0: not apart, and the orders differ there) and trace_twin (a write by thread 1 and a barrier of its block of threads: not apart, and after a write by thread 0 one order is racy and the other not).

  4. The ops of two threads' steps are apart (Mem.Op.At.apart, for every memory and all ops): for threads w, w' with different numbers, if their liveness changes are at blocks of m each owns, their sets at different numbers, and no write one logs overlaps a write the other logs. A put of one and a set of the other are at different blocks, as the put's block exists and the set's does not; a liveness change of one is at a block owned by it, whose owner admits no other thread, so not at the other's put or liveness change; two puts at one block are at disjoint bytes, as a common byte would be written by a write of each; two logs are accesses, which are independent.

    theorem Mem.Op.At.apart {m : Mem} {w w' : Who} {L L' N N' : Nat → Prop}
        {W W' : List Event} (hw : w.thread ≠ w'.thread) (hL : ∀ b, L b → m.Owned w.thread b)
        (hL' : ∀ b, L' b → m.Owned w'.thread b) (hN : ∀ b, N b → ¬N' b)
        (hW : ∀ a a', .access a ∈ W → .access a' ∈ W' → a.overlaps a' = true →
          a.write = false ∨ a'.write = false) :
        ∀ {o o' : Mem.Op}, o.At m w L N W → o'.At m w' L' N' W' → o.Apart o'
    

    In a grid whose threads own their allocas (Grid.Owns) the blocks a step's frames allocated are blocks it owns, and the fresh numbers of two threads differ (Mem.fresh_inj, by Nat.pair_eq_pair).

  5. The case (Grid.advance_comm). Grid.advance_agree lifts step_agree to the grid: n's step from g gives a status s and ops at those places with g' = {threads := g.threads.set! n s, mem := g.mem.run ops}, and from every grid with n's thread at n and a memory m₂ that agrees with g.mem for n, the same s and ops on m₂. Grid.advance_agree_other: in a grid whose threads own their allocas, k's step leaves the memory agreeing for a thread n with another number at the numbers g.mem knows and n's fresh number, if no write of k overlaps an access of n (by Mem.Changes.agree with Grid.advance_changes). In a closed grid those numbers are all n's scope names (Grid.Closed.scope), so n's step from gₖ is n's ops on gₖ.mem, and symmetrically; by Mem.run_comm and Array.setIfInBounds_comm the two grids are Grid.Same:

    theorem Grid.advance_comm {c : Nat → Ctx} {g gn gk : Grid} {n k : Nat}
        (hg : g.Closed c) (ho : g.Owns c) (hw : (c n).who.thread ≠ (c k).who.thread)
        (hn : g.advance c n = .ok (some gn)) (hk : g.advance c k = .ok (some gk))
        (hW : ∀ a a', .access a ∈ g.pending c n → .access a' ∈ g.pending c k →
          a.conflict a' = false) :
        ∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧
          g₁.Same g₂
    

    For every grid, every context map and all threads n, k. n ≠ k follows from hw. The premise hW asks that no pending access of either conflict with one of the other (Access.conflict). If some write of one overlaps an access of the other, the two are an exempt pair, two atomicrmws of one commuting operation at one pointer and size, and Grid.advance_atomic_comm gives the conclusion (L10, below). Otherwise no write of either overlaps any access of the other (not only the other's writes), as the agreement of L2 needs, and the argument above applies.

The instance (MojoProofTest/Confluence.lean diamond): on stores 4, threads 0 and 1 storing 7 and 8 to bytes 0 to 3 and 4 to 7 of one block, the premises hold and the two orders give the same grid. The twins: diamond_twin (on stores 0 both store to bytes 0 to 3, two plain writes that conflict, so hW fails, and the two orders give grids that differ at byte 0); who_twin (two threads at alloca with one thread number in their contexts: both take block 2, and after either step the other's faults); owns_twin (a thread whose frame lists block 1, of every thread, among its allocas, so the grid is not Grid.Owns: its return makes block 1 dead, after which the other thread's store to block 1 faults, while in the other order both succeed). Closed.pending_twin shows a grid that is not closed in which one thread's step changes another's pending accesses.

From the start (Grid.start_reach_comm, Confluence.lean): the start's contexts give thread n the program and the number n (Grid.start_ctx, Grid.lean), and every grid reachable from the start's is closed and owns its allocas (Grid.start_reach_closed, Grid.start_owns, Grid.reach_owns), so for every program, kernel, launch, arguments and choices on a caller's memory with m.BytesIn m.Known:

theorem Grid.start_reach_comm {prog : Program} {fn : Func} {L : Launch} {args : List Val}
    {m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g g' gn gk : Grid}
    {c : Nat → Ctx} {n k : Nat} (h : Grid.start prog fn L args m ch oracle = .ok (g, c))
    (hm : m.BytesIn m.Known) (hr : Relation.ReflTransGen (Grid.Step L c) g g')
    (hnk : n ≠ k) (hn : g'.advance c n = .ok (some gn))
    (hk : g'.advance c k = .ok (some gk))
    (hW : ∀ a a', .access a ∈ g'.pending c n → .access a' ∈ g'.pending c k →
      a.conflict a' = false) :
    ∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧
      g₁.Same g₂

Its instance (start_diamond): the launch on two threads of alloca i32, a store of 7 to it and ret void; after both allocas (blocks 2 and 4) the two stores commute. The twin of n ≠ k (start_twin): in the launch of MojoProofTest/Launch.lean thread 0's return from g4 succeeds and accesses nothing, but after it thread 0 takes no step, so the conclusion fails for n = k = 0. The twin of hm is Closed.start_twin (a memory that passes the start's check and gives a grid that is not closed); that of hW is diamond_twin.

The case of a thread and a collective call in Lean (done for every grid). Grid.advance_fire_comm (Confluence.lean), for every launch, contexts and grid, at a barrier or a shuffle, with no premise of closedness or ownership:

theorem Grid.advance_fire_comm {L : Launch} {c : Nat → Ctx} {g gk gf : Grid} {n k : Nat}
    (hk : g.advance c k = .ok (some gk)) (hf : g.fire L c n = .ok (some gf))
    (hw : .barrier (c k).who.cta ∉ g.fired L n) :
    ∃ g₁ g₂, gk.fire L c n = .ok (some g₁) ∧ gf.advance c k = .ok (some g₂) ∧
      g₁.Same g₂

Grid.fired L g n is [.barrier (n / size)] if n waits at a barrier and [] otherwise, so at a barrier hw says that k's context is not of n's block of threads, and at a shuffle it holds.

The proof: k runs (Grid.advance_set). So k is not a participant: at a barrier a running thread of n's block makes the barrier give none, and at a shuffle a running lane of n's warp makes parts.mapM id none (Grid.fire_frame, by mapM_ok_mem and mapM_id_mem). The call changes no thread but its participants (resumeAll_getElem?) and no block. The participants' results go through withAttrs on the memory, which reads it only by Mem.addr, and so only at the shape of a block that exists. A step keeps every block's shape (Grid.advance_keeps, W5), so what the call gives on g's memory it gives on the memory after k's step (Mem.addr_keeps, withAttrs_keeps, resumeAll_set). No closedness is needed: only success moves from one memory to the other. So each order gives g with k's new status, the participants resumed and k's memory, with traces Grid.fired L g n in front of k's accesses or behind them (Grid.advance_retrace). Every one of those accesses is by (c k).who (Thread.pending_who), and every event fired is .barrier (n / size) (Grid.mem_fired), so by hw the two are independent and the traces are Trace.Same (L11).

Its instances (MojoProofTest/Confluence.lean):

The twin of hw (fire_twin): fire_diamond's grid in contexts that put both threads in block of threads 0. After a write by thread 0 of the bytes thread 1 stores to, one order's trace is racy and the other's not, so the grids are not the same.

The case of a thread and a collective call from the start (done). Grid.start_reach_fire_comm (Confluence.lean), for every program, kernel, launch, arguments, caller's memory, address choice and oracle, in every grid reachable from the start's, at a barrier or a shuffle, with no premise on blocks of threads:

theorem Grid.start_reach_fire_comm {prog : Program} {fn : Func} {L : Launch} {args : List Val}
    {m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g₀ g gk gf : Grid} {c : Nat → Ctx}
    {n k : Nat}
    (h : Grid.start prog fn L args m ch oracle = .ok (g₀, c))
    (hr : Relation.ReflTransGen (Grid.Step L c) g₀ g)
    (hk : g.advance c k = .ok (some gk)) (hf : g.fire L c n = .ok (some gf)) :
    ∃ g₁ g₂, gk.fire L c n = .ok (some g₁) ∧ gf.advance c k = .ok (some g₂) ∧
      g₁.Same g₂

The proof: hw of Grid.advance_fire_comm. If the call fires a .barrier, it is n's (Grid.mem_fired), and k is not among n's block's threads n / size * size + j, j < size (Grid.fire_frame). The start's contexts give k the block of threads k / size (Grid.start_ctx). If k / size = n / size, then k = n / size * size + k % size would be one of them, with k % size < size when size > 0. And size > 0, as k is a thread (Grid.start_reach_block). That holds because the start has a thread only if size > 0 (Grid.start_run), a step sets one thread, and a fire resumes threads at numbers that had one (Grid.fire_threads).

Its instance (start_fire_diamond): the kernel of MojoProofTest/Launch.lean (an alloca, a store, barrier 0, ret void) on two blocks of one thread, with thread 0 at the barrier and thread 1's store next. The twin of the start's contexts (start_fire_twin): fire_twin's contexts are no start's, since the start of its launch puts thread 1 in block of threads 1.

The case of two collective calls in Lean (done). Grid.fire_fire_comm (Confluence.lean), for every launch, contexts and grid, at barriers and shuffles alike, if the two threads' contexts have one layout:

theorem Grid.fire_fire_comm {L : Launch} {c : Nat → Ctx} {g gn gj : Grid} {n j : Nat}
    (hl : (c n).prog.layout = (c j).prog.layout)
    (hn : g.fire L c n = .ok (some gn)) (hj : g.fire L c j = .ok (some gj)) :
    gn = gj ∨
      ∃ g₁ g₂, gn.fire L c j = .ok (some g₁) ∧ gj.fire L c n = .ok (some g₂) ∧ g₁.Same g₂

The first alternative is the diamond's degenerate case: the two fires are one step of Grid.Step. Grid.start_fire_fire_comm gives the same for every grid in the start's contexts, for every program, kernel, launch, arguments, caller's memory and choices, since the start gives every thread the one program (Grid.start_ctx).

The proof rests on Grid.fire_frame: a call that completes for n has a list P of participants, each waiting (at a barrier, n's block of threads n / size * size + j, j < size), with results v, such that

Then:

A launch whose block of threads has no threads (size = 0) has no participant at a barrier, so n ∉ P. The case split covers it by j ∈ P ∨ n ∈ Q.

Its instances (MojoProofTest/Confluence.lean):

The twin of hl (layout_twin): two lanes at a shuffle of a nonnull pointer at address 2 ^ 32, thread 0 with 64-bit pointers and thread 1 with 32-bit ones. Thread 0's fire keeps the pointer, thread 1's makes it poison, and after either fire no thread waits, so neither alternative holds.

The premise on accesses from ownership in Lean (done for every grid). Grid.pending_apart (Confluence.lean) is the disjointness argument above, for the two threads' pending accesses: for every discipline (T, own), contexts and grid, hH at the two threads and hX at their contexts' threads give hW.

theorem Grid.pending_apart {ι : Type} {T : Nat → Option (Tiling ι)} {own : Nat → ι}
    {c : Nat → Ctx} {g gn gk : Grid} {n k : Nat}
    (hn : g.advance c n = .ok (some gn)) (hk : g.advance c k = .ok (some gk))
    (hX : own (c n).who.thread ≠ own (c k).who.thread)
    (hH : ∀ p ∈ [n, k], ∀ a, .access a ∈ g.pending c p →
      Owned T own a ∨ g.mem.Owned (c p).who.thread a.ptr.block) :
    ∀ a a', .access a ∈ g.pending c n → .access a' ∈ g.pending c k →
      a.overlaps a' = true → a.write = false ∧ a'.write = false

The proof, for overlapping a of n and a' of k (so of one block):

From the start (Grid.start_reach_comm_of_owned): Grid.start_reach_comm with hW from Grid.pending_apart through Access.conflict_eq_false (accesses that overlap only if neither writes do not conflict), hX as own n ≠ own k (the start gives thread n the number n, Grid.start_ctx), and the premise n ≠ k from it.

theorem Grid.start_reach_comm_of_owned {ι : Type} {T : Nat → Option (Tiling ι)}
    {own : Nat → ι} {prog : Program} {fn : Func} {L : Launch} {args : List Val}
    {m : Mem} {ch : Nat → Nat} {oracle : Nat → Nat → Nat} {g g' gn gk : Grid}
    {c : Nat → Ctx} {n k : Nat} (h : Grid.start prog fn L args m ch oracle = .ok (g, c))
    (hm : m.BytesIn m.Known) (hr : Relation.ReflTransGen (Grid.Step L c) g g')
    (hX : own n ≠ own k)
    (hn : g'.advance c n = .ok (some gn)) (hk : g'.advance c k = .ok (some gk))
    (hH : ∀ p ∈ [n, k], ∀ a, .access a ∈ g'.pending c p →
      Owned T own a ∨ g'.mem.Owned p a.ptr.block) :
    ∃ g₁ g₂, gn.advance c k = .ok (some g₁) ∧ gk.advance c n = .ok (some g₂) ∧
      g₁.Same g₂

hH here is at the two threads of one grid. hH above, at every running thread of every reachable grid, gives it. hI is not needed: invariant loads are reads.

Its instances (MojoProofTest/Confluence.lean):

The twins (apart_twins), on stores 0, where hW and the conclusion fail (diamond_twin):

Two atomicrmws of one commuting operation on the same bytes (L10, done): Access.conflict exempts them, so Grid.advance_comm covers them, but they follow no discipline with different tiles, so Grid.start_reach_comm_of_owned does not. The steps, in Lean: Mem.load_store (Confluence.lean), for every layout, memory, two threads, type, value, pointer and two alignments, gives that a load after a store that succeeds, at the same pointer, by a thread and with an alignment whose check succeeds before the store, gives what the stored bytes decode to, and Mem.load_store_codec that for a codec (integers, floats) this is the stored value. So in the diamond the second thread's load, which succeeds from g, reads what the first stored. Instances store_load, store_load_other; twins store_load_i0 (a poison i0 loads back as bits 0 0) and load_other (of the check). rmw_comm_of_ok (Rmw.lean) gives the final value from a scalar for all operands, if both updates succeed from it, since an operand need not be a scalar. Mem.load_scalar gives that the loaded value of an integer type is Val.Scalar (instance load_scalar; twin load_ptr), and Layout.scalar_round that a scalar of a positive width round-trips through its bytes (instance undef_round; twins poison_i0, ptr_i32). From these, Mem.update_comm: for every two layouts, memory, threads, spaces, operands and alignments, every operation and every positive width n, two updates (Mem.update, what exec does at an atomicrmw) of type .int n at one pointer that each succeed from m succeed in both orders and give the same blocks, next and trace up to swaps (instance update_comm, two adds). Then exec_atomic, step_atomic and Grid.advance_atomic: a step that succeeds and would log an atomic access e is, from every memory with the same shape, the update at e's pointer of type .int (8 * e.len) (Stmt.checkAtomic_int) by an operand that is a scalar of that width (exec checks it); and Grid.advance_atomic_comm, for every closed grid and contexts: two threads n ≠ k whose steps succeed, with pending accesses by atomicrmws of one operation at one pointer and size, give the same grid in both orders (Grid.Same) (instance atomic_comm, two atomicrmw adds at one word; with Grid.advance_comm, atomic_diamond).

bad_step (P, faults persist). Let g fault at n and let g → g'.

Conclusion 2. By induction on reachability:

Two accesses of different threads to one block are therefore both owned. They lie in different tiles (hX), so they do not race; this is racy_eq_false_of_owned with thread-local accesses added, a small generalization. An invariant load is on an untiled block (hI), which no access writes (Owned.read), so kept holds (kept_of_split).

In detail, the invariant, for every grid g reachable from g₀:

At the start the trace is empty. A step of thread n (Grid.advance_set) keeps the size of the threads and logs Grid.pending c g n in front of the trace. Each of these accesses is by (c n).who (Grid.pending_who), whose thread is n (Grid.start_ctx), below N since n is a thread of g; it is to a block whose owner admits n (Grid.advance_admits); hH gives the third part, and hI the fourth, since an owner .thread w that admits n is .thread n. The step keeps every block's owner (Grid.start_reach_changes, Mem.Changes.old), so the accesses already in the trace keep theirs. A fire logs no access (Grid.fire_trace), changes no block (Grid.fire_blocks) and adds no thread (Grid.fire_threads).

From the invariant: a clash of the trace (racy_iff) is of two accesses of different threads to one block. If the block's owner is a .thread w, both threads are w (the owner admits them); otherwise both accesses are owned, and Owned.apart with hX makes both reads, which do not conflict (racy_eq_false_of_local, with "local to w" being g.mem.Owned w). For kept (the core's at sig), kept_of_split with W b := T b ≠ none ∨ ∃ w, g.mem.Owned w b: an access that writes for the core (a write or an atomicrmw's read) has a write with its pointer in the trace, which is shaped (Trace.Shaped.writes, Grid.start_reach_shaped); that write is owned, so to a tiled block (Owned.read), or thread-local; an invariant load is to an untiled block with no thread owner.

The enabled race: if g has a running thread n with racy (Thread.pending (c n) t g.mem ++ g.mem.trace) = true and n's step succeeds, the grid after is reachable and its trace is that list (Grid.advance_set), which is not racy by the above; a running thread's step is never none (Grid.advance_run), so it faults.

Conclusion 3 in Lean

Statement (Grid.owned_sound, MojoProof/Sem/ScheduleOwned.lean). For every program, kernel, launch, arguments, address choice, oracle and discipline (T, own), given hs, T2's hm, an observation obs with obs m = obs m' whenever m.blocks = m'.blocks, a run Path (Grid.Step L c) n g₀ z with GPUProof.Done sig (z, L, c), a predicate loc b w ("block b is local to thread w"), and obs z.mem = some y with R y, if the trace tr := z.mem.trace meets

then Grid.Sound L c g₀ obs R.

Proof.

  1. A done grid is bad only by its trace (Grid.Done.bad_iff, every launch, contexts and grid with at most the launch's number of threads; Grid.start_reach_size gives z exactly that many). The core's Bad at sig is Faults ∨ racy ∨ ¬ kept ∨ RaceEnabled. A fault is a thread's step or fire that errs, and in a done grid every thread is .done or absent, so Grid.advance and Grid.fire return .ok none (done_not_faults). An enabled race needs a thread .run t or a fire that completes (Grid.raceEnabled_iff), which a done grid has not (Grid.Done.not_raceEnabled).
  2. The trace lemma (racy_kept_of_local, every launch and every trace whose oldest-first form is shaped, Trace.Shaped tr.reverse). racy tr = false is racy_eq_false_of_local from hO, hL and hX. For the core's kept at sig, let W b say that some write of tr is to block b. Every access that writes for the core is to a block of W: a write by definition, an atomicrmw's read by the write right after it (Trace.Shaped.writes); every access with !invariant.load is to a block not in W by hI; that is kept_of_split.
  3. So z is not bad: by 1, a bad z has racy tr = true or a broken promise, and 2 refutes both, its shape from Grid.start_reach_shaped. Grid.start_sound (T2's conclusion 2) then gives Grid.Sound from the run, hs, hm and the observation's premises.

Relation to the statement above. The hypotheses hH, hI and hX of T2′ give the trace premises at every reachable grid with loc b w := g.mem.Owned w b (the argument of Grid.owned_clean: an owned block's owner admits only its thread, and a write that follows the discipline is to a tiled block, which an access with !invariant.load is not to). The Lean form asks them of one trace only, which a checker evaluates on one run.

Instance and twin (MojoProofTest/ScheduleOwned.lean, one kernel each). cells_sound: the run of cellK i16 in which thread 0 takes its four steps and then thread 1, with T := cells, own := tileOf and no local block; the trace premises are a Boolean check on the done grid's trace (traceB, sound for every discipline and trace by traceB_sound), and the observation is bytes 0 to 3 of block 1, 7 and 7 as 16-bit words. cells_sound_twin: in cellK i8 the same schedule reaches a done grid with a racy trace, so it is bad, no (T, own, loc) meets the trace premises (they would make it not racy), and the start's grid is sound for no observation.

The trace lemma of an elementwise kernel

Statement (racy_kept_of_cells, MojoProof/Sem/Trace.lean, every trace tr, output block out, width w and cell map cell : Nat → Nat with Apart cell w, that is, for threads i ≠ j, cell i + w ≤ cell j or cell j + w ≤ cell i). If

then, at every launch L, tr has no race when it is by threads of L in their blocks of threads and shaped (the core's racy at sig, racy_iff's premises), and the core's kept at sig keeps the trace's core events (GPUProof.kept sig ((tr.map (Event.toCore L)).reverse) = true). This is the premise (hU → Trace.Shaped m'.trace.reverse → GPUProof.racy sig ((m'.trace.map (Event.toCore L)).reverse) = false) ∧ GPUProof.kept sig ((m'.trace.map (Event.toCore L)).reverse) = true of a sequential run's soundness, for a kernel in which each thread writes only its own cells of one output block and reads blocks that nobody writes. It covers cells at any offsets w bytes apart (a row-major tensor from any base offset) and accesses inside a cell (a narrower load or store of it). racy_kept_pointwise (same file: cells k * w, each access the whole cell, threads below a bound) is its corollary, with Apart from apart_stride.

Proof.

  1. kept. Let W b say b = out. Every access that writes for the core (a write or an atomicrmw's read, Access.kind_writes) is to out: one to another block contradicts hr. Every access with !invariant.load is to a block other than out by hi. That is kept_of_split.
  2. racy. Suppose racy tr = true. By racy_iff there are two accesses x, y of the phases of tr, so of tr (mem_phases), that clash, which needs (among its conditions) different threads i ≠ j, one block, overlapping bytes (x.off < y.off + y.len and y.off < x.off + x.len), and at least one write.
    • If the block is out, by hc x's bytes are inside [cell i, cell i + w) and y's inside [cell j, cell j + w). By Apart these two intervals are disjoint, so x and y do not overlap: a contradiction (linear arithmetic).
    • Otherwise both accesses are reads by hr, and a clash needs a write: a contradiction.

Instance and twins (MojoProofTest/ScheduleOwned.lean, statements about one trace). elemTr o: two threads, each an !invariant.load of its 4 bytes of block 2 and a store of 4 bytes at offset o n of block 1. elem_clean (every launch): with o n = 4 n + 8 and cells 4 n + 8 of width 4 (apart by linear arithmetic, an offset racy_kept_pointwise does not cover), the premises hold for each of the four accesses by evaluation, so the trace is not racy and keeps the promise. elem_owned: the same trace follows the tiling elemCells (block 1 in two cells of 4 bytes from 8), so racy_eq_false_of_owned also gives that it is not racy. elem_twin_cell (the cell premise): with both stores at offset 8 the trace is racy. elem_twin_inv (the !invariant.load premise): after a further !invariant.load of bytes 8 to 11 of block 1, which thread 0 stores, kept is false.

Route in Lean

T2 (Grid.confluent) gives Confluent rc Same Bad on γ from the start alone, with the core's Bad at sig. A general lemma of the core, Confluent.transfer (GPUProof/Transfer.lean), moves confluence along a surjection that maps the steps, the equivalence and the bad set; along the identity it replaces the bad set of a confluent system by one with the same members. Conclusion 2 makes the core's Bad and the faults (GPUProof.Faults at sig) agree on γ (Grid.owned_bad), so conclusion 1 is Grid.confluent with Confluent.transfer along the identity (Grid.confluent_owned). This route needs hm : m.BytesIn m.Known (T2's hypothesis) and none of the lemmas of the sections above that T2 does not already use.

Dependencies

Instances and twins

Given (MojoProofTest/ScheduleOwned.lean, one kernel each, by evaluation of every reachable grid with the checker every, whose soundness every_sound holds for every launch):

Not given here: