aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / bootstrap / formal-oracles / lean / AienKv / Theorems.lean
| 1 | import AienKv.Model |
| 2 | namespace AienKv |
| 3 | |
| 4 | /-! Rules enforced by the code (AIEN.INV.KV.LAYOUT_BOUNDS.V1). Every theorem is for every config. -/ |
| 5 | |
| 6 | /-- scaling a bound: `i < n` gives `i*s + s ≤ n*s` -/ |
| 7 | theorem succ_mul_le {i n s : Nat} (h : i < n) : i * s + s ≤ n * s := by |
| 8 | have := Nat.mul_le_mul_right s (show i + 1 ≤ n from h) |
| 9 | rw [Nat.succ_mul] at this; exact this |
| 10 | |
| 11 | /-! ### Nested bounds, innermost first -/ |
| 12 | |
| 13 | theorem elem_in_head (c : Cfg) {d : Nat} (h : d < c.headDim) : d * c.elemBytes + c.elemBytes ≤ c.head := |
| 14 | succ_mul_le h |
| 15 | |
| 16 | theorem head_in_token (c : Cfg) {h : Nat} (hh : h < c.numKvHeads) : h * c.head + c.head ≤ c.token := |
| 17 | succ_mul_le hh |
| 18 | |
| 19 | theorem token_in_plane (c : Cfg) {t : Nat} (ht : t < c.blockSize) : t * c.token + c.token ≤ c.plane := |
| 20 | succ_mul_le ht |
| 21 | |
| 22 | theorem plane_in_layer (c : Cfg) {v : Nat} (hv : v < 2) : v * c.plane + c.plane ≤ c.layer := by |
| 23 | have := succ_mul_le (s := c.plane) hv |
| 24 | unfold Cfg.layer; exact this |
| 25 | |
| 26 | theorem layer_in_block (c : Cfg) {l : Nat} (hl : l < c.numLayers) : l * c.layer + c.layer ≤ c.block := |
| 27 | succ_mul_le hl |
| 28 | |
| 29 | theorem block_in_total (c : Cfg) {b : Nat} (hb : b < c.numBlocks) : b * c.block + c.block ≤ c.total := |
| 30 | succ_mul_le hb |
| 31 | |
| 32 | /-- THE BOUNDS THEOREM. A valid (block, layer, K/V, token, head, dim) names an element whose bytes |
| 33 | `[off, off + elemBytes)` lie inside the allocation `[0, total)`. -/ |
| 34 | theorem element_in_allocation (c : Cfg) {b l v t h d : Nat} (hv : c.Valid b l v t h d) : |
| 35 | InRange (c.off b l v t h d) c.elemBytes c.total := by |
| 36 | obtain ⟨hb, hl, hv2, ht, hh, hd⟩ := hv |
| 37 | have e1 := elem_in_head c hd |
| 38 | have e2 := head_in_token c hh |
| 39 | have e3 := token_in_plane c ht |
| 40 | have e4 := plane_in_layer c hv2 |
| 41 | have e5 := layer_in_block c hl |
| 42 | have e6 := block_in_total c hb |
| 43 | unfold InRange Cfg.off |
| 44 | omega |
| 45 | |
| 46 | /-- the token-plane address used by `write_token_kv` / `read_token_kv` is also inside, with room for a full |
| 47 | `numKvHeads * headDim` row -/ |
| 48 | theorem token_row_in_allocation (c : Cfg) {b l v t : Nat} (hb : b < c.numBlocks) (hl : l < c.numLayers) |
| 49 | (hv : v < 2) (ht : t < c.blockSize) : InRange (c.tokOff b l v t) c.token c.total := by |
| 50 | have e3 := token_in_plane c ht |
| 51 | have e4 := plane_in_layer c hv |
| 52 | have e5 := layer_in_block c hl |
| 53 | have e6 := block_in_total c hb |
| 54 | unfold InRange Cfg.tokOff |
| 55 | omega |
| 56 | |
| 57 | /-- `block_region`: block b occupies `[b*block, b*block + block)` inside the allocation -/ |
| 58 | theorem block_region_in_allocation (c : Cfg) {b : Nat} (hb : b < c.numBlocks) : |
| 59 | InRange (b * c.block) c.block c.total := block_in_total c hb |
| 60 | |
| 61 | /-! ### Disjointness -/ |
| 62 | |
| 63 | /-- distinct blocks have disjoint regions -/ |
| 64 | theorem block_regions_disjoint (c : Cfg) {b1 b2 : Nat} (h : b1 ≠ b2) : |
| 65 | Disjoint (b1 * c.block) c.block (b2 * c.block) c.block := by |
| 66 | unfold Disjoint |
| 67 | rcases Nat.lt_or_gt_of_ne h with h | h |
| 68 | · left; exact succ_mul_le h |
| 69 | · right; exact succ_mul_le h |
| 70 | |
| 71 | /-- distinct layers of the same block have disjoint, in-block regions -/ |
| 72 | theorem layer_regions_disjoint (c : Cfg) {l1 l2 : Nat} (h : l1 ≠ l2) : |
| 73 | Disjoint (l1 * c.layer) c.layer (l2 * c.layer) c.layer := by |
| 74 | unfold Disjoint |
| 75 | rcases Nat.lt_or_gt_of_ne h with h | h |
| 76 | · left; exact succ_mul_le h |
| 77 | · right; exact succ_mul_le h |
| 78 | |
| 79 | /-- every layer region lies inside its block -/ |
| 80 | theorem layer_region_in_block (c : Cfg) {l : Nat} (hl : l < c.numLayers) : |
| 81 | InRange (l * c.layer) c.layer c.block := layer_in_block c hl |
| 82 | |
| 83 | /-- K and V planes of one layer are disjoint (K ends exactly where V starts) -/ |
| 84 | theorem k_v_planes_disjoint (c : Cfg) (l : Nat) : |
| 85 | Disjoint (l * c.layer + 0 * c.plane) c.plane (l * c.layer + 1 * c.plane) c.plane := by |
| 86 | unfold Disjoint; left; omega |
| 87 | |
| 88 | /-- distinct tokens of one plane have disjoint rows -/ |
| 89 | theorem token_rows_disjoint (c : Cfg) {t1 t2 : Nat} (h : t1 ≠ t2) : |
| 90 | Disjoint (t1 * c.token) c.token (t2 * c.token) c.token := by |
| 91 | unfold Disjoint |
| 92 | rcases Nat.lt_or_gt_of_ne h with h | h |
| 93 | · left; exact succ_mul_le h |
| 94 | · right; exact succ_mul_le h |
| 95 | |
| 96 | /-- the stride chain itself is consistent: block = layers * 2 * planes and plane = tokens * heads * dim * bytes -/ |
| 97 | theorem block_closed_form (c : Cfg) : |
| 98 | c.block = 2 * c.numLayers * c.blockSize * c.numKvHeads * c.headDim * c.elemBytes := by |
| 99 | unfold Cfg.block Cfg.layer Cfg.plane Cfg.token Cfg.head |
| 100 | simp only [Nat.mul_assoc, Nat.mul_left_comm, Nat.mul_comm] |
| 101 | |
| 102 | /-- `bytes_per_block` (2 * layers * block_size * heads * dim * element size) is the same number -/ |
| 103 | theorem block_eq_bytes_per_block (c : Cfg) : |
| 104 | c.block = 2 * c.numLayers * c.blockSize * c.numKvHeads * c.headDim * c.elemBytes := block_closed_form c |
| 105 | |
| 106 | /-! ### Pool constructor: `buffer.len() >= total_bytes` -/ |
| 107 | |
| 108 | /-- if the host buffer is at least `total` bytes (the check in `UnifiedKvTensorPool::new`), every valid |
| 109 | element's bytes lie inside the host buffer -/ |
| 110 | theorem element_in_buffer (c : Cfg) {b l v t h d bufLen : Nat} (hv : c.Valid b l v t h d) |
| 111 | (hbuf : c.total ≤ bufLen) : InRange (c.off b l v t h d) c.elemBytes bufLen := by |
| 112 | have := element_in_allocation c hv |
| 113 | unfold InRange at *; omega |
| 114 | |
| 115 | /-! ### 64-bit (machine) arithmetic: when the natural-number result is the machine result -/ |
| 116 | |
| 117 | def two64 : Nat := 18446744073709551616 |
| 118 | |
| 119 | /-- all six dimensions are nonzero (a zero dimension has no valid element at all) -/ |
| 120 | def Cfg.Pos (c : Cfg) : Prop := |
| 121 | 1 ≤ c.numBlocks ∧ 1 ≤ c.blockSize ∧ 1 ≤ c.numLayers ∧ 1 ≤ c.numKvHeads ∧ 1 ≤ c.headDim ∧ 1 ≤ c.elemBytes |
| 122 | |
| 123 | /-- With nonzero dimensions the strides grow along the chain head <= token <= plane <= layer <= block <= total. |
| 124 | So if `total < 2^64`, no stride product wraps and the unbounded model is exact for the Rust `usize` code. -/ |
| 125 | theorem machine_exact_when_total_fits (c : Cfg) (hp : c.Pos) (ht : c.total < two64) : |
| 126 | c.head < two64 ∧ c.token < two64 ∧ c.plane < two64 ∧ c.layer < two64 ∧ c.block < two64 := by |
| 127 | obtain ⟨hnb, hbs, hnl, hnh, _, _⟩ := hp |
| 128 | have h1 : c.block ≤ c.total := Nat.le_mul_of_pos_left _ hnb |
| 129 | have h2 : c.layer ≤ c.block := Nat.le_mul_of_pos_left _ hnl |
| 130 | have h3 : c.plane ≤ c.layer := by unfold Cfg.layer; omega |
| 131 | have h4 : c.token ≤ c.plane := Nat.le_mul_of_pos_left _ hbs |
| 132 | have h5 : c.head ≤ c.token := Nat.le_mul_of_pos_left _ hnh |
| 133 | omega |
| 134 | |
| 135 | /-- and every valid offset (and each summand of it) is below total, hence below 2^64 -/ |
| 136 | theorem offset_fits_u64 (c : Cfg) {b l v t h d : Nat} (hv : c.Valid b l v t h d) (ht : c.total < two64) : |
| 137 | c.off b l v t h d < two64 := by |
| 138 | have := element_in_allocation c hv |
| 139 | unfold InRange at this; omega |
| 140 | |
| 141 | /-- THE LIMITATION, stated: the unbounded model and the Rust code disagree as soon as `total ≥ 2^64`. |
| 142 | `from_config` does not refuse such a config (no checked arithmetic). -/ |
| 143 | def wrapTotal (c : Cfg) : Nat := c.total % two64 |
| 144 | |
| 145 | theorem wrap_total_smaller (c : Cfg) (h : two64 ≤ c.total) : wrapTotal c < c.total := by |
| 146 | unfold wrapTotal two64 at *; omega |
| 147 | |
| 148 | end AienKv |