---------------------------- MODULE ChunkedCommit ----------------------------
\* One chunked decode step after another (src/engine.rs encode_decode_chunked,
\* encode_spans_x's flush, finish_decode_chunks / chunk_statuses,
\* discard_decode_chunks; src/serve.rs, the decode step). The host writes the
\* step's descriptors (DecodeSpanDesc, DecAttnGroup in the shared arena), then
\* encodes the layers into chunks 1..N, committing each as it is encoded, so the
\* GPU starts on chunk 1 while the host still encodes chunk 2. The caller's
\* buffer (index N + 1, Sampling: the GPU top-k or Gumbel sampling) is committed
\* last. The one queue runs buffers in commit order; any buffer may fault, and
\* a later buffer still runs after an earlier one faults. The host waits for the
\* tail, bounded: on completion it drains every chunk's status (the step is good
\* only if all completed), on timeout it fails the step and discards the chunks.
\* encode_decode_chunked asserts the previous step's chunks were drained.
\*
\* Reversed witnesses (each fails the named invariant):
\* Bug = "lastonly": judge the step by the tail's status alone -> OkImpliesAllDone
\* Bug = "earlytail": the tail may be committed before the last
\* chunk (sampling before the logits head) -> SampleAfterChunks
\* Bug = "nodrain": the timeout path keeps the chunks -> DrainedBeforeEncode
\* Bug = "latewrite": descriptors written after chunk 1 commits -> NoWriteRace
EXTENDS Naturals, Sequences
CONSTANTS N, Steps, Bug
ASSUME N \in 1..4 /\ Steps \in 1..3 /\ Bug \in {"none", "lastonly", "earlytail", "nodrain", "latewrite"}
Sampling == N + 1
Bufs == (1..Steps) \X (1..Sampling)
VARIABLES step, k, wrote, tailc, pc, queue, status, executed, early, verdict, race
vars == <<step, k, wrote, tailc, pc, queue, status, executed, early, verdict, race>>
Init == /\ step = 1 /\ k = 1 /\ wrote = FALSE /\ tailc = FALSE /\ pc = "encode"
/\ queue = <<>> /\ status = [b \in Bufs |-> "none"] /\ executed = <<>>
/\ early = {} /\ verdict = [s \in 1..Steps |-> "none"] /\ race = FALSE
Pending(s) == \E i \in 1..Sampling : status[<<s, i>>] = "committed"
\* the host's writes into GPU-visible memory for this step (before the first chunk; "latewrite" also after it)
Write == /\ pc = "encode" /\ ~wrote
/\ (k = 1 \/ (Bug = "latewrite" /\ k = 2))
/\ wrote' = TRUE
/\ race' = (race \/ Pending(step))
/\ UNCHANGED <<step, k, tailc, pc, queue, status, executed, early, verdict>>
\* encode chunk k's layers and commit it (flush, or the end of encode_spans_x for chunk N)
Commit == /\ pc = "encode" /\ k <= N
/\ (wrote \/ (Bug = "latewrite" /\ k = 1))
/\ queue' = Append(queue, <<step, k>>)
/\ status' = [status EXCEPT ![<<step, k>>] = "committed"]
/\ early' = early \cup {<<step, k>>}
/\ k' = k + 1
/\ UNCHANGED <<step, wrote, tailc, pc, executed, verdict, race>>
\* the caller encodes sampling into its buffer and commits it
CommitTail == /\ pc = "encode" /\ ~tailc /\ wrote
/\ (k = N + 1 \/ (Bug = "earlytail" /\ k = N))
/\ queue' = Append(queue, <<step, Sampling>>)
/\ status' = [status EXCEPT ![<<step, Sampling>>] = "committed"]
/\ tailc' = TRUE
/\ UNCHANGED <<step, k, wrote, pc, executed, early, verdict, race>>
StartWait == /\ pc = "encode" /\ tailc /\ k = N + 1
/\ pc' = "wait"
/\ UNCHANGED <<step, k, wrote, tailc, queue, status, executed, early, verdict, race>>
\* the GPU runs the queue's head to completion or a fault
Gpu == /\ queue /= <<>>
/\ \E r \in {"done", "fault"} : status' = [status EXCEPT ![Head(queue)] = r]
/\ executed' = Append(executed, Head(queue))
/\ queue' = Tail(queue)
/\ UNCHANGED <<step, k, wrote, tailc, pc, early, verdict, race>>
\* wait_committed_step_bounded returns, then finish_decode_chunks drains every chunk
Complete == /\ pc = "wait" /\ status[<<step, Sampling>>] \in {"done", "fault"}
/\ LET judged == IF Bug = "lastonly" THEN {<<step, Sampling>>}
ELSE {<<step, i>> : i \in 1..Sampling}
IN verdict' = [verdict EXCEPT ![step] =
IF \A b \in judged : status[b] = "done" THEN "ok" ELSE "fail"]
/\ early' = {}
/\ pc' = "next"
/\ UNCHANGED <<step, k, wrote, tailc, queue, status, executed, race>>
\* the bounded wait times out: fail_step, and discard_decode_chunks
Timeout == /\ pc = "wait" /\ status[<<step, Sampling>>] = "committed"
/\ verdict' = [verdict EXCEPT ![step] = "fail"]
/\ early' = IF Bug = "nodrain" THEN early ELSE {}
/\ pc' = "next"
/\ UNCHANGED <<step, k, wrote, tailc, queue, status, executed, race>>
NextStep == /\ pc = "next" /\ step < Steps
/\ step' = step + 1 /\ k' = 1 /\ wrote' = FALSE /\ tailc' = FALSE /\ pc' = "encode"
/\ UNCHANGED <<queue, status, executed, early, verdict, race>>
Done == pc = "next" /\ step = Steps /\ UNCHANGED vars
Next == Write \/ Commit \/ CommitTail \/ StartWait \/ Gpu \/ Complete \/ Timeout \/ NextStep \/ Done
Spec == Init /\ [][Next]_vars
TypeOK == /\ status \in [Bufs -> {"none", "committed", "done", "fault"}]
/\ verdict \in [1..Steps -> {"none", "ok", "fail"}]
/\ early \subseteq Bufs
\* a step reported good had every one of its buffers complete
OkImpliesAllDone == \A s \in 1..Steps : verdict[s] = "ok" => \A i \in 1..Sampling : status[<<s, i>>] = "done"
\* sampling runs only after every chunk of its step (the logits head is in chunk N)
SampleAfterChunks == \A j \in 1..Len(executed) : executed[j][2] = Sampling =>
\A i \in 1..N : \E j2 \in 1..(j - 1) : executed[j2] = <<executed[j][1], i>>
\* encode_decode_chunked's assert: no earlier step's chunks are still held
DrainedBeforeEncode == (pc = "encode" /\ k = 1) => early = {}
\* the host never writes descriptors while a buffer of the step may be reading them
NoWriteRace == ~race
=============================================================================