interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / bootstrap / formal-oracles / lean / AienKv / Model.lean

59 lines · 2662 bytes Raw Blame History
1/-!
2# Abstract model of the pooled KV tensor layout (DEVELOPMENT_ORACLE)
3
4Source modelled: aien-dev/sovereign-core `crates/aien-kv-cache/src/lib.rs` at dd1fe32,
5`KvLayout::from_config` (stride arithmetic), `element_offset`, `token_plane_offset`,
6`block_offset`, and `UnifiedKvTensorPool::{new, block_region}`.
7
8Invariant: AIEN.INV.KV.LAYOUT_BOUNDS.V1 (formal/invariants/kv-layout-bounds-v1.toml).
9
10Abstractions (each is an ASSUMPTION, not a proof):
11* Arithmetic is on unbounded naturals. The Rust code uses `usize` with NO checked arithmetic.
12 The theorems in `Theorems.lean` (section "64-bit") state exactly when the natural
13 result equals the machine result: every product stays below 2^64, equivalently `total < 2^64`
14 when `numBlocks ≥ 1`. A concrete wrap witness is in `Corpus.lean`, and was reproduced in a release
15 build of the real crate (see DISCREPANCIES.md).
16* Bytes are a half-open range `[start, start + len)`. "Within allocation" means the range lies in
17 `[0, total)`.
18* `elemBytes` is the dtype width in bytes (Fp8 1, Bf16/Fp16 2, Fp32 4). Fp4 is refused by the code
19 and is not modelled.
20* Alignment and the host buffer's own address are not modelled. The pool constructor's check
21 `buffer.len() >= total_bytes` is modelled as the hypothesis `bufLen ≥ total`.
22-/
23namespace AienKv
24
25structure Cfg where
26 numBlocks : Nat
27 blockSize : Nat -- tokens per block
28 numLayers : Nat
29 numKvHeads : Nat
30 headDim : Nat
31 elemBytes : Nat
32
33/-- Rust `KvLayout::from_config`, same order of operations. -/
34def Cfg.head (c : Cfg) : Nat := c.headDim * c.elemBytes
35def Cfg.token (c : Cfg) : Nat := c.numKvHeads * c.head
36def Cfg.plane (c : Cfg) : Nat := c.blockSize * c.token
37def Cfg.layer (c : Cfg) : Nat := 2 * c.plane
38def Cfg.block (c : Cfg) : Nat := c.numLayers * c.layer
39def Cfg.total (c : Cfg) : Nat := c.numBlocks * c.block
40
41/-- Rust `element_offset` (is_value = 0 for K, 1 for V). -/
42def Cfg.off (c : Cfg) (b l v t h d : Nat) : Nat :=
43 b * c.block + l * c.layer + v * c.plane + t * c.token + h * c.head + d * c.elemBytes
44
45/-- Rust `token_plane_offset`. -/
46def Cfg.tokOff (c : Cfg) (b l v t : Nat) : Nat :=
47 b * c.block + l * c.layer + v * c.plane + t * c.token
48
49/-- All indices valid (the Rust asserts, in `element_offset`). -/
50def Cfg.Valid (c : Cfg) (b l v t h d : Nat) : Prop :=
51 b < c.numBlocks ∧ l < c.numLayers ∧ v < 2 ∧ t < c.blockSize ∧ h < c.numKvHeads ∧ d < c.headDim
52
53/-- A byte range `[s, s+n)` lies inside `[0, total)`. -/
54abbrev InRange (s n total : Nat) : Prop := s + n ≤ total
55
56/-- Two byte ranges do not overlap. -/
57abbrev Disjoint (s1 n1 s2 n2 : Nat) : Prop := s1 + n1 ≤ s2 ∨ s2 + n2 ≤ s1
58
59end AienKv