Docs / Proofs

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:

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.

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

Instances in the tree

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:

  1. Block.holds_writes: if block blk holds x (Block.Holds), then writing bs i at off i for the cells of is, in the order of is (repetitions allowed), gives a block that holds the tensor y with y i = f i for i ∈ is and y i = x i otherwise.
  2. Mem.tensor_writes: a memory whose block b is that block observes some y at b.
  3. Mem.tensor_vec: the case of shape [N], binary32 (.fp .float, Val.ofFloat .float) at row-major offsets from base, and is the cells below n (i ∈ is ↔ i < n): the observation is i ↦ w i below n and x i from n on.
  4. Mem.tensor_vec_add: in 3 with every cell stored (n = N) and w i = deliver (a i + c i) (k i), which is what fadd with no flags gives on the words of a i and c i for the oracle number 3 k i (floatBin_ofFloat), and no a i + c i a NaN: the observation is some (a + c), where + on tensors is TorchLean's Add (Tensor α s) (zipWith (· + ·)) and on elements FloatLib's binary32 addition, the instance Add (Model fmt). No copy of either is defined.

Proofs.

  1. Writes change no block's size (Block.size_puts, by induction on the list), so each tile still lies in the block. The encodings have l.stored t bytes (the codec's round trip), and the tiles are apart, so after the writes the l.stored t bytes at off i are bs i if i ∈ is and the block's own otherwise (Block.read_puts). In the first case they decode to enc (f i) (the codec's round trip), in the second to enc (x i) (blk holds x). Both are enc (y i) (Tensor.Rep.get_ofFn).
  2. Through a codec a block holds at most one tensor, so the observation is y (Mem.tensor_eq).
  3. Binary32 words are a codec (Layout.Codec.float) and row-major offsets are apart (apart_rowMajor); rewrite i ∈ is by i < n.
  4. By 3, the observation is i ↦ deliver (a i + c i) (k i) (every cell is below N). 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:

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.