171 lines
9.7 KiB
Markdown
171 lines
9.7 KiB
Markdown
# Orientation: what you're looking at
|
||
|
||
If you come from vLLM, llama.cpp, SGLang, or TensorRT-LLM, this repo will feel
|
||
alien at first. That's expected — it's built on a different mental model. This
|
||
document explains what `vetta-infer` is, why it looks the way it does, and the
|
||
road ahead. Read this before diving into the code.
|
||
|
||
## What it is, in one paragraph
|
||
|
||
`vetta-infer` is a **MoE-first microkernel inference engine** written in safe
|
||
Rust with a minimal set of AMD ROCm HIP kernels. It is **not** a general-purpose
|
||
engine: it targets one specific machine — an AMD Ryzen AI 9 HX 370 APU
|
||
(`gfx1150`, RDNA 3.5) with **96 GiB of unified memory** and dual NVMe — and one
|
||
class of models: **Mixture-of-Experts decoder LMs** (first target:
|
||
Inkling-2-Mini). Small dense *utility encoders* (e.g. BGE-M3 embeddings) are in
|
||
scope as a secondary lane; dense decoder LMs are deliberately **out of scope**.
|
||
The engine is co-designed with its consumer, the `vetta-agents` framework
|
||
(evolved from `strata-ctl`), and ultimately serves a principal named Iris.
|
||
|
||
## Why it looks different (the mental-model shift)
|
||
|
||
Five ideas explain almost everything that will surprise you:
|
||
|
||
1. **Pure microkernel + services.** There is no monolithic runtime. A tiny
|
||
kernel (`kn`) owns the bus, registry, and UMA/DMA broker and contains *zero
|
||
model logic*. Everything else — loading, compilation, execution, sampling,
|
||
speculative decoding, telemetry, the wire gateway — is a separate service
|
||
crate. Control flow stays in safe, CPU-testable Rust; `unsafe` is confined to
|
||
the HIP FFI (`hip`) and IO syscalls (`io`).
|
||
|
||
2. **Models are compiled, not loaded.** A dual-step loader turns a HuggingFace
|
||
`.safetensors` model into a canonical IR, then a `BackendPacker` lowers it
|
||
into a portable **`.vbundle/`** (manifest + weights + plans). You don't point
|
||
the engine at raw weights; you build a bundle once and boot from it. The
|
||
manifest carries identity-by-provenance (sha256), ABI version, and target.
|
||
|
||
3. **The forward pass is a declared, validated plan.** Instead of imperative
|
||
kernel calls scattered through C++ files, the model's compute is expressed as
|
||
`graph.vplan` (an op sequence) + `memory.vplan` (buffer arenas). At boot these
|
||
lower to a flat `CompiledPlan`. **Before any GPU launch**, the plan is checked
|
||
by an arena validator (no buffer overlap, checked bounds, no
|
||
use-before-write) whose correctness is **proven in Lean 4**. The graph is
|
||
data, not code.
|
||
|
||
4. **Proof-first for complex logic.** Non-trivial state machines — the DMA slab
|
||
ring, bus lifecycle, serve session, arena plans — are modeled and verified in
|
||
`proofs/*.lean` (0 `sorryAx`, standard axioms only) *before* the Rust is
|
||
written. Simple logic just gets ordinary CPU tests. This is a hard rule, not
|
||
a nicety: it eliminates whole classes of bugs by construction.
|
||
|
||
5. **Parity discipline, no unmeasured claims.** Every kernel is checked against
|
||
a CPU oracle (and the hardened `strata` C++/HIP reference) before it is wired
|
||
in. Every performance claim must carry real measurements and the exact
|
||
hardware/model/flags. "It should be fast" is not a sentence we accept.
|
||
|
||
## The crate map
|
||
|
||
| Crate | Responsibility |
|
||
| --- | --- |
|
||
| `vetta-infer-abi` | Shared contracts: `.vbundle` manifest/plans, vplan opcodes, arena validator, JSON, sha256, serve wire types. |
|
||
| `vetta-infer-kn` | The microkernel: bus, registry, UMA/DMA broker. No model logic. |
|
||
| `vetta-infer-ld` | Loader: safetensors reader, ModelImporter (config → IR → graph), BackendPacker, bundle I/O. |
|
||
| `vetta-infer-hip` | HIP FFI/DMA backend (gfx1150). `unsafe` isolated here. Feature-gated kernel build. |
|
||
| `vetta-infer-cc` | Compilation/control services. |
|
||
| `vetta-infer-rt` | Operational runtime: executes the `CompiledPlan` on the HIP backend. |
|
||
| `vetta-infer-lab` | Laboratory/probing runtime; shares the `rt` executor core. |
|
||
| `vetta-infer-sp` | Speculative decoding / MTP verification. |
|
||
| `vetta-infer-nt` | NVMe streaming / disk cache (io_uring, write-once `O_RDONLY`). |
|
||
| `vetta-infer-tm` | Telemetry. |
|
||
| `vetta-infer-gw` | Wire gateway: the stdin/stdout line protocol the agent speaks; CLI. |
|
||
|
||
## Current state (honest)
|
||
|
||
- **The engine runs real inference, end-to-end, and it is correct.** The full
|
||
48-visit Inkling-2-Mini forward (prelude + looped core ×3 passes with bank
|
||
rollback + coda + final depth-mix/softcap) runs on the real `gfx1150` weights.
|
||
Greedy decoding is **EXACT multi-token parity** against the hardened `strata`
|
||
reference (16 and 40 tokens, F32 and FP16 KV). The two root causes of the old
|
||
token-2+ gap were structural, not precision: per-pass KV pools and the
|
||
unpadded logit vocab (200058).
|
||
- **Linked to the agent framework.** `strata-ctl --serve` spawns `vetta-infer`
|
||
and serves its OpenAI-compatible API; the wire protocol (READY/INFO/T/PP/
|
||
RESUME/DONE/ERR) is field-for-field compatible. Verified live: capital queries,
|
||
Italian multi-turn, EOS/stop handling.
|
||
- **First real interactive GUI conversation (2026-12-01, measured).** Through the
|
||
`strata-ctl` web GUI on Sogliola (`http://sogliola:1234`), engine `vetta-infer`:
|
||
- prompt *"Ciao, sono Federico"* → *"Ciao Federico! Piacere di conoscerti.
|
||
Come posso aiutarti oggi?"*
|
||
- **2650 prompt + 53 completion tokens · 4.2 tok/s decode · first token 618 s.**
|
||
- The 618 s time-to-first-token is the **sequential prefill** of the GUI's
|
||
~2650-token agent prompt (~140 ms/token); decode itself is ~4.2 tok/s. This
|
||
is the honest current performance frontier, not a hang.
|
||
- **Host CPU during inference is no longer a busy-wait.** ROCm's default spin
|
||
host-wait schedule pinned the process at ~100% CPU; setting
|
||
`hipDeviceScheduleBlockingSync` before context creation (as the reference does)
|
||
dropped it to ~54% (measured A/B 101.2% → 54.2%). The residual ~54% is genuine
|
||
kernel-launch submission cost (~1300 launches/token), removed only by HIP graph
|
||
capture.
|
||
- **Multi-turn prefill is no longer re-paid: the prefix checkpoint cache landed
|
||
(2026-12-01, measured).** Ported in concept from strata `--auto-ckpt` and proven
|
||
in `proofs/prefix_cache.lean` (`restore_equiv`, 0 `sorryAx`), `serve_gen` restores
|
||
the longest cached prefix of a prompt and prefills only the NEW suffix tokens;
|
||
the checkpoint saves at `pos` (prompt + generated tokens) so the next turn reuses
|
||
the whole prior context. Pinned anchors {512,1024} are never evicted (I3), LRU at
|
||
cap 6, KV snapshots unpooled (freed on eviction). **Parity EXACT** (restore ==
|
||
cold re-prefill, token-for-token, both prewarm- and decode-state checkpoints).
|
||
Measured on `gfx1150`, Inkling-2-Mini, FP16 KV, greedy: a 605-token prompt with a
|
||
600-token cached prefix prefills in **2.29 s vs 132.5 s cold (57.8×)**, end-to-end
|
||
**3.2 s vs 133.2 s (41.8×)**. This collapses the *second-turn* time-to-first-token;
|
||
the *first* turn still pays sequential prefill (batched prefill is the next brick).
|
||
- **What is NOT done yet (the frontier):** HIP graph capture (collapse the
|
||
per-token launches → near-zero host CPU), batched/chunked prefill (M>1 attention
|
||
+ grouped MoE, make the *first* large-prompt time-to-first-token seconds, not
|
||
minutes), and the expert-GEMV math speedup (WMMA). These are the M2 performance
|
||
work — correctness is already there; speed is the next axis.
|
||
|
||
## The road to the goal
|
||
|
||
The destination: a working, measured inference engine that the agent framework
|
||
can talk to over the wire, replacing the old `strata` engine for a real
|
||
conversation. **Steps 1–5 are landed** — the first real GUI conversation happened
|
||
on 2026-12-01 (see *Current state*). The path, in order:
|
||
|
||
1. **Inkling graph parity** — ✅ landed (attention-residual block-mix, loop
|
||
controllers, ShortConv4, relative-position bias, sliding-window attention,
|
||
per-pass KV). Blueprint: `docs/inkling-forward-design.md`.
|
||
2. **Remaining kernels** — ✅ landed (flash GQA/SWA, MoE NVFP4/MXFP8 routing +
|
||
experts + reduce, KV-write, L2-clip, relpos). Each parity-verified.
|
||
3. **Executor** — ✅ landed (full 48-visit forward on real weights).
|
||
4. **Weights → device** — ✅ landed (resident `WeightCache`, lazy expert load).
|
||
5. **Serve wiring + first chat** — ✅ landed (`strata-ctl` link, EXACT parity,
|
||
first real GUI conversation 2026-12-01).
|
||
6. **M2 performance** — ⏳ the current frontier: prefix checkpoint cache ✅ landed
|
||
(multi-turn prefill reuse, EXACT parity, 57.8× measured); HIP graph capture
|
||
(host CPU), batched/chunked prefill (first-turn time-to-first-token), and
|
||
expert-GEMV WMMA (decode tok/s) still pending. Measured against the reference,
|
||
no unmeasured claims.
|
||
7. **M3 Lyra co-processor** — remote drafting, semantic domain routing,
|
||
domain-aware expert prefetch.
|
||
|
||
## How to verify
|
||
|
||
```sh
|
||
# CPU workspace tests (host-independent, no GPU/toolchain needed):
|
||
RUSTFLAGS="-D warnings" cargo test --workspace
|
||
|
||
# Lean proofs (0 sorryAx, standard axioms):
|
||
lean proofs/slab_ring_dma.lean
|
||
lean proofs/bus_lifecycle.lean
|
||
lean proofs/serve_session.lean
|
||
lean proofs/arena_plan.lean
|
||
lean proofs/prefix_cache.lean
|
||
|
||
# Opt-in live tests against the real Inkling artifact:
|
||
cargo test -p vetta-infer-ld -- --ignored inkling
|
||
|
||
# GPU kernel parity (requires gfx1150 + ROCm; builds kernels via hipcc):
|
||
cargo test -p vetta-infer-hip --features hip -- --ignored
|
||
```
|
||
|
||
## Where to read next
|
||
|
||
- `docs/ARCHITECTURE.md` — the decision-complete spec (microkernel, services,
|
||
`.vbundle`, plans, wire contract, milestones).
|
||
- `docs/inkling-forward-design.md` — the Inkling forward pass, extracted from
|
||
the reference, that the graph emitter follows.
|
||
- `proofs/*.lean` — the verified state machines and arena plan.
|
||
- `AGENTS.md` — the engineering rules and hard invariants.
|
||
|
||
Welcome. Take your time with the mental model first; the code reads much more
|
||
naturally once the five ideas above click. |