inky-engine 2.0.4

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple M5 Macs
----------------------------- MODULE SplitArena -----------------------------
EXTENDS Integers, FiniteSets

\* Layout of the tensor path's prefill attention partials (src/engine.rs:
\* fixed_split_count, split_scratch_slots, fixed_attn_nsplit, prefill_split_slots,
\* fixed_attn_batch, BArena::new; src/kernels.metal: fixed-split attention and
\* combine), with the same arithmetic at a small scale. One span (p0, t) of one
\* layer kind runs its query rows [ts, t) in batches. Each batch dispatches every
\* (row, head, split) with split < nsplit whose first key j0 lies before the
\* row's end, writing slot (row * NH + head) * nsplit + split past the region's
\* base; the combine then reads the row's Count visible splits from the same
\* slots. The activation buffer holds, in order: the decode arena's partials
\* (ar.part, NH * Slots), its logits (ar.logits, one address here), the rest of
\* the batch arena, then the batch arena's own prefill partials (b.part,
\* NH * PSlots), then the next region (the decode batch lanes).
CONSTANT Capacity, PCap, Chunk, Bug
NH == 2            \* query heads
KpsSwa == 2        \* window split width (64 in production)
KpsFull == 4       \* full split width (256 / 512)
W == 4             \* window, EXT (1024); a multiple of KpsSwa as in production
MaxCtx == 8        \* the engine's context limit
ASSUME /\ Capacity \in {3, 4, 6}
       /\ PCap \in 1..32            \* PREFILL_PART_BYTES / (NH * (HD + 2) * 4)
       /\ Chunk \in 1..MaxCtx       \* the batch arena's capacity in tokens (ensure_batch)
       /\ Bug \in {"none", "edgeDropped", "rowsUnbounded", "decodePart", "unfloored"}

Min(a, b) == IF a < b THEN a ELSE b
Max(a, b) == IF a > b THEN a ELSE b
CeilDiv(a, b) == (a + b - 1) \div b
\* fixed_split_count(first, end, width)
SplitCount(first, end, w) == IF first = end THEN 0 ELSE (end - 1) \div w + 1 - first \div w
\* split_scratch_slots(max_ctx): Capacity stands for the 256-slot floor
Slots == Max(Capacity, CeilDiv(MaxCtx, KpsFull) + 1)
\* fixed_attn_nsplit(swa, end): the reserved split stride, with the edge split once the window slides
NSplitOf(sw, end, edge) == IF sw /\ end > W
                           THEN CeilDiv(W - 1, KpsSwa) + edge
                           ELSE SplitCount(0, end, IF sw THEN KpsSwa ELSE KpsFull)
\* prefill_split_slots(max_ctx, chunk)
NeedSplit == Max(NSplitOf(TRUE, MaxCtx, 1), NSplitOf(FALSE, MaxCtx, 1))
PSlots == IF Bug = "unfloored" THEN Min(PCap, Chunk * NeedSplit)
          ELSE Max(Slots, Min(PCap, Chunk * NeedSplit))
\* addresses (one per partial) in the activation buffer
DecPart == 0                       \* ar.part
Logits == NH * Slots               \* ar.logits, right after ar.part
PBase == Logits + 2                \* b.part, after the rest of both arenas
PEnd == PBase + NH * PSlots        \* the next region (decode batch lanes)

VARIABLES swa, p0, t, start, phase, arena
vars == <<swa, p0, t, start, phase, arena>>

Kps == IF swa THEN KpsSwa ELSE KpsFull
SpanEnd == p0 + t
NSplit == NSplitOf(swa, SpanEnd, IF Bug = "edgeDropped" THEN 0 ELSE 1)
Rows == IF Bug = "rowsUnbounded" THEN PSlots ELSE PSlots \div NSplit
N == Min(t - start, Rows)
\* part_off: b.part (decodePart: the decode arena's ar.part, as before the dedicated region)
Base == IF Bug = "decodePart" THEN DecPart ELSE PBase

\* row r of the span (0-based) as the kernels see it
End(r) == p0 + r + 1
First(r) == IF swa /\ End(r) > W THEN End(r) - W ELSE 0
Count(r) == SplitCount(First(r), End(r), Kps)
Owner(i, h, sp) == <<p0 + start + i, h, First(start + i) \div Kps + sp>>
Slot(i, h, sp) == Base + (i * NH + h) * NSplit + sp

\* grid (nsplit, heads, rows); returns when j0 >= end
Dispatched == {<<i, h, sp>> \in (0..(N - 1)) \X (0..(NH - 1)) \X (0..(NSplit - 1)) :
                  (First(start + i) \div Kps + sp) * Kps < End(start + i)}
WSlot(c) == Slot(c[1], c[2], c[3])
\* combine: the row's visible splits in increasing order
Reads == UNION {{<<i, h, s>> : s \in 0..(Count(start + i) - 1)} : <<i, h>> \in (0..(N - 1)) \X (0..(NH - 1))}

Init ==
    /\ swa \in BOOLEAN
    /\ p0 \in 0..(MaxCtx - 1)
    /\ t \in 1..Min(Chunk, MaxCtx - p0)  \* encode_spans: t <= ba.cap
    /\ start \in 0..(t - 1)             \* ts: tail trimming starts the query rows late
    /\ phase = "attn"
    /\ arena = [s \in {} |-> <<>>]

Attend ==
    /\ phase = "attn"
    /\ arena' = [s \in DOMAIN arena \cup {WSlot(c) : c \in Dispatched} |->
                    IF \E c \in Dispatched : WSlot(c) = s
                    THEN LET c == CHOOSE c \in Dispatched : WSlot(c) = s IN Owner(c[1], c[2], c[3])
                    ELSE arena[s]]
    /\ phase' = "comb"
    /\ UNCHANGED <<swa, p0, t, start>>
Combine ==
    /\ phase = "comb"
    /\ start' = start + N
    /\ phase' = IF start + N >= t THEN "done" ELSE "attn"
    /\ UNCHANGED <<swa, p0, t, arena>>
Done == phase = "done" /\ UNCHANGED vars

Next == Attend \/ Combine \/ Done
Spec == Init /\ [][Next]_vars /\ WF_vars(Attend) /\ WF_vars(Combine)

\* the loop advances: step_by(0) would panic, and an empty batch never finishes
RowsPositive == Rows >= 1
\* no partial is written outside the prefill region (into the decode arena or the lanes that follow it)
WritesInArena == \A s \in DOMAIN arena : PBase <= s /\ s < PEnd
\* the decode arena's logits (the last decode step's, still read by the scheduler) are never overwritten
DecodeLogitsIntact == Logits \notin DOMAIN arena
\* one dispatch never writes one slot twice
WritesDisjoint == \A c1, c2 \in Dispatched : c1 # c2 => WSlot(c1) # WSlot(c2)
\* every partial the combine reads is this batch's own, for its row, head and absolute split
CombineReadsOwn ==
    phase = "comb" =>
        \A r \in Reads : LET s == Slot(r[1], r[2], r[3]) IN
            /\ PBase <= s /\ s < PEnd
            /\ s \in DOMAIN arena
            /\ arena[s] = Owner(r[1], r[2], r[3])
Finishes == <>(phase = "done")
=============================================================================