soft3/glia/run/specs/execution.md

Execution contract

How a loaded .model turns into output tokens / audio samples / image pixels. The runtime's public API and internal dispatch rules.

Load phase

model = cyb_llm::load(path)

Steps:

  1. mmap the .model file (read-only).
  2. Parse text sections (config, tensors, vocab).
  3. Validate config schema and tensor completeness.
  4. Identify execution path:
    • model_type matches curated family → curated
    • else if graph section present → graph
    • else → error
  5. Instantiate curated family code OR graph executor.
  6. Upload weights to backend (or defer to first forward for lazy GPU).
  7. Pre-compute position caches (cos/sin for RoPE).

Load must be idempotent: loading same file twice produces equivalent models (weights may or may not share GPU memory depending on backend).

Forward phase

Text generation

generator = model.generator()
for tokens in generator.generate(prompt, max_tokens=N, sampling=...):
    ...

Under the hood:

tokens = tokenizer.encode(apply_chat_template(prompt))
for t in tokens:
    logits = model.forward(&[t])        # prefill: one call per prompt token
for step in 0..max_tokens:
    next_token = sample(logits, sampling)
    if eos_tokens.contains(next_token): break
    emit(next_token)
    logits = model.forward(&[next_token])  # decode: append to KV

Prefill and decode use the same forward function with seq_len=1. Optimization: bulk prefill (seq_len > 1) available but must produce identical output to token-by-token (ε tolerance in ops.md).

Encoder (embeddings / classification)

h = model.encode(tokens)              # returns final hidden states
emb = pool(h, method)                  # cls / mean / max
# or
logits = model.classify(tokens)        # returns classification logits

Whisper (ASR)

mel = audio_to_mel(samples, model.config)
h_enc = model.encoder(mel)
tokens = model.decode(h_enc, prompt_tokens=None)  # autoregressive decoder
text = tokenizer.decode(tokens)

Diffusion (image/video)

latent = randn(shape=model.config.latent_shape)
for t in noise_schedule:
    noise_pred = model.forward(latent, t, text_emb)
    latent = denoise_step(latent, noise_pred, t)
image = vae.decode(latent)

TTS

phonemes = text_frontend(text)
mel = model.text_to_mel(phonemes, speaker_id=...)
audio = model.mel_to_audio(mel)   # vocoder

Backend dispatch

Each op dispatched to a backend. The chosen backend for a given op:

Platform default

Platform Default
macOS + Apple Silicon honeycrisp
macOS + Intel wgpu+rs
Linux / Windows / Android / Web wgpu+rs

Fallback chain

For a single op, given the chosen default backend:

  1. Try default backend's supports(op, inputs). If yes → execute there.
  2. If default is honeycrisp and rejects, try wgpu+rs.
  3. If wgpu+rs rejects, fall back to CPU reference library.
  4. CPU reference implements every op — step 3 always succeeds.

The runtime may report per-op dispatch decisions to observability so heavy CPU fallback is visible.

User override

cyb-llm run --backend=wgpu+rs|honeycrisp|nox forces the entire execution to a single backend. Override skips steps 1-2; if op is not supported, fall-through is still to CPU library (step 3).

--backend=nox only works if model has ~~~graph section (graph executor is Tier 1 for nox, not curated).

Backend trait

pub trait Backend: Send + Sync {
    /// Backend identity for logging and dispatch records.
    fn name(&self) -> &str;

    /// Return true if this backend implements the op for the given
    /// input dtypes/shapes. If not, caller falls back to CPU reference.
    fn supports(&self, op: &Op, inputs: &[TensorInfo]) -> bool;

    /// Execute one op. Inputs are already on the backend's memory
    /// (or shared via zero-copy for honeycrisp). Output may allocate
    /// or reuse from the frame allocator.
    fn execute(
        &self,
        op: &Op,
        inputs: &[Tensor],
        ctx: &mut ExecContext,
    ) -> Result<Vec<Tensor>, BackendError>;

    /// Allocate a tensor on this backend's memory.
    fn alloc(&self, shape: &[usize], dtype: DType) -> Result<Tensor, BackendError>;

    /// Upload f32/quantized bytes from host memory.
    fn upload(&self, data: &[u8], shape: &[usize], dtype: DType) -> Result<Tensor, BackendError>;

    /// Download to host memory for inspection.
    fn download_f32(&self, t: &Tensor) -> Vec<f32>;
}

Memory lifecycle

Weights (long-lived)

Allocated at load time. Persistent for the lifetime of the model instance. Freed when model dropped.

KV cache (per-conversation)

Allocated at conversation start, sized to max context. Appended to on each decode step. Cleared on reset_kv_cache().

Activations (per-step)

Frame allocator: per-backend pool of buffers keyed by (size, usage). On each forward call, the allocator hands out buffers from the pool or allocates new ones. At the start of the NEXT forward call, all buffers from the previous call become available for reuse (they're guaranteed unused by pending GPU work if dispatch was complete).

pub struct FrameAllocator {
    pool: HashMap<(u64 /* size */, BufferUsage), Vec<Buffer>>,
    in_use: Vec<(u64, BufferUsage, Buffer)>,
}

impl FrameAllocator {
    pub fn alloc(&mut self, size: u64, usage: BufferUsage) -> Buffer;
    pub fn reset(&mut self);  // called at start of next forward
}

Sizing policy:

  • Buckets by exact byte size. No over-allocation to "next power of 2" — different shapes in different layers produce many bucket sizes, but reuse within a size is perfect.
  • On reset, all in-use buffers move back to the pool. Pool grows monotonically to the steady-state size, then flat.
  • Typical footprint for a 7B decoder at bs=1: ~200 MB of pool.

Separate pools per BufferUsage (STORAGE vs UNIFORM on wgpu; IOSurface vs shared memory on honeycrisp).

See llm/src/backend/wgpu/alloc.rs for the existing wgpu implementation. Honeycrisp allocator follows the same design with IOSurface-backed buffers from aruminium.

Position caches

Cos/sin cache for RoPE: allocated at load, sized to max_position_embeddings × half_dim. Read-only during forward.

Error semantics

Errors are structured, not strings:

pub enum ExecutionError {
    UnsupportedOp { op: String, backend: String, input_dtype: DType },
    ShapeMismatch { op: String, expected: Vec<usize>, got: Vec<usize> },
    MissingTensor { name: String, required_by: String },
    WeightFormatMismatch { name: String, declared: DType, actual_bytes: usize },
    OutOfMemory { backend: String, requested_bytes: usize },
    BackendInit { backend: String, reason: String },
    InvalidConfig { field: String, reason: String },
    Tokenizer { reason: String },
}

Every error is actionable — the user can tell which backend, which op, which tensor. No "internal error" or generic panic.

Silent corruption is a BUG. If a backend returns wrong output without erroring, that's higher severity than a missing-op error (which at least surfaces).

Invariants across paths

Running the same model with the same input through different paths must produce outputs within the tolerances in ops.md:

output_curated  ≈ output_graph  ≈ output_nox  (within ε)
output_wgpu+rs  ≈ output_honeycrisp            (within ε)

A divergence larger than ε between paths or backends indicates a bug in one of them. The CPU reference is the arbiter.

Concurrency

Multiple conversations

Each conversation has its own KV cache. Multiple cached conversations can share one loaded model (same weights, separate KV).

Not supported in v1: batched inference (multiple prompts processed in one forward pass). Future.

Thread safety

Backend is Send + Sync. Running inference from multiple threads on different models is safe. Running multiple concurrent forwards on the same model is ALLOWED only if each has its own KV cache; shared KV is a race.

Observability

Every forward exposes timing breakdown (load-time, prefill-ms, per-step-ms) for performance analysis. Backends log which op went to which device (e.g., "layer 5 attention: honeycrisp.ane, FFN: honeycrisp.gpu").

Shutdown

drop(model)

Frees all GPU/backend memory. Must be idempotent. No leaks.

Fused op policy

Fused ops (ops.md) are optional optimizations. Policy:

Curated paths

Each curated family codepath explicitly emits fused ops where beneficial. The author knows the architecture and writes fused calls directly:

let (normed, res) = fused_skip_norm(hidden, attn_out, post_norm_w, eps);

No auto-detection. Curated code is hand-tuned; the author chose the fusion.

Graph path

The graph executor runs a pre-execution fusion pass that rewrites the graph. Recognized patterns:

Pattern Fused op
RmsNorm → Matmul FusedNormMatmul
Add → RmsNorm (both outputs used) FusedSkipNorm
Matmul → Silu followed by Matmul + Mul FusedSwiGlu
Sdpa (when backend has FlashAttention) FlashAttention

Detection is purely structural: a subgraph matches the pattern's shape. If the backend reports supports(FusedSkipNorm, ...) is true, the fused node replaces the sub-DAG. Else stays unfused.

Fusion is applied at graph-load time, once. Subsequent forwards use the fused graph.

Correctness

Every fused op must produce the same output as its unfused equivalent within ε (ops.md). Violation is a backend bug — the graph executor can fall back to unfused.

Extension points

Adding a new backend: implement Backend trait, register in dispatch table. The runtime automatically routes supported ops. Curated codepaths opt in per-family.

Adding a new op: add Op enum variant, math spec in ops.md, CPU reference implementation, at least one backend implementation. Version bump per scope.md.

Adding a new model family: write a curated codepath implementing the graph from arch.md. Register the model_type string(s) that dispatch to it.

Homonyms

execution
Execution A neuron executes admitted progs through the existing Rune machine. Work pins code, state revision, input, context and a finite allowance. Ward supplies current host authority independently of those data. The exact persisted fields belong to [data](/soft3/neuron/specs/data) and [runtime…
neural/rune/specs/execution
three execution modes [← specification index](/neural/rune/specs/readme) pure — proven by construction every reduction in the pure subset has a Nox trace, which has a zheng proof. reactive — event-driven computation a `hint` yields execution until matching data arrives. the kernel is a pure…
soft3/radio/audit/2026-10-13-audit-cargo-packaging/execution
execution
soft3/radio/audit/2026-10-12-bao-bounded-proof/task/execution
Approved execution Implementation parent: Radio b933829cbe273d2f31fe75ca83125f3ddd8917a2. The immutable PLAN input is 8ae88f7dd21b5cc00228737e3cfb2abbb1e471810e5b524fd2f700cc7e45868f. Fresh Opus PLAN approved without blockers at 2026-10-12T23:47:49.084499Z; raw…
soft3/radio/.claude/tasks/audit-cargo-packaging/execution
Packaging execution Stage: approved; final handoff to root. Fresh Opus PLAN v2 approved at 2026-10-13 01:58:51 UTC; raw SHA256 `27a51f83e0ea67a2fbd7951f8bf83a2572ea4fe859a95d99a33d90f233a7dec4`. Root read the verdict and independently replayed the frozen input. V1/v2 packets, both raw reviews and…
soft3/radio/.claude/tasks/bao-bounded-proof/execution
Approved execution Implementation parent: Radio b933829cbe273d2f31fe75ca83125f3ddd8917a2. The immutable PLAN input is 8ae88f7dd21b5cc00228737e3cfb2abbb1e471810e5b524fd2f700cc7e45868f. Fresh Opus PLAN approved without blockers at 2026-10-12T23:47:49.084499Z; raw…
warriors/erga/.claude/tasks/blake-batch-owner/execution
Blake batch owner execution Stage active: implementation and scoped gates complete; fresh different-vendor CODE review APPROVE received. Research and plan remain the original reviewed snapshots. This execution note records later state without rewriting review inputs. Serves Kadek foundation row 6's…
soft3/radio/.claude/tasks/bao-bounded-traversal/execution
Bounded traversal — pre-implementation evidence Current parent is Radio `a96d86e28d384cdf656aadc4282c78c0c170c102`, the actual PR38 merge. The ordinary fast-forward preserves this task's untracked artifacts. The 11 Git command records / 22 raw streams in…
soft3/nox/.claude/tasks/hemera-no-std/execution
Hemera feature-boundary execution Approved input: PLAN v2 `1f2028db98726cea04732856de00b2b058501b930112df0811f09c83c1ac60d3`. Fresh Opus 5 APPROVE completed `2026-10-13T02:23:59.183394Z`; raw stdout `1bd21c68c348163df8bd58a0205772732e50dfc733d30374aa61cdf88d09c223`. Invocation/completion/full raw…
soft3/radio/.claude/tasks/bao-bounded-reducer/execution
Bounded BAO group reducer — execution Serves Kadek phase-1 foundation row 4 through Radio/soft3. Parent is `dacd030c9d6301b316a96b5bc6e4ea1e1cec79f1`; Hemera remains pinned to `d5f0a09539360934fb32db9b2e15a91995607db6`. This calculates hashes only. No authenticated particle/root association, proof…
warriors/erga/.claude/tasks/rtable-owner/execution
RTable owner execution Stage active; serves Kadek foundation row 6, discovered-from Honeycrisp PR14 and Erga PR1. Implementation base is Erga `14b40d667b7fa927f62f46423cd8096f01cf704f`, Honeycrisp `9cbd48070a136e24711896c930670fcff8a147fc`, Strata `494df7a0973ae10a6cc1c2022e644d70449a29d6`.…
soft3/radio/.claude/tasks/kadek-memstore-pairs/execution
Execution notes Fresh PLAN v2 APPROVE received; source scope remains the approved two store writers. The original immutable PR32 right-proof.bin will be copied byte-identically into iroh-blobs/src/store/mem/parent_pairs/right-proof.bin for package-safe include_bytes!. This preserves the approved…
warriors/erga/.claude/tasks/mine-owner/execution
Mine owner execution 2026-10-11: resumed frozen PLAN-only task at origin/main 8dcb33888868d74ddfda3eeeb9bc90e58677b394. Independent replay verified 162 inputs, 82 exact Git bodies and 871 archive files; origins unchanged. PLAN pack SHA256…
soft3/radio/.claude/tasks/bao-hemera-pin/execution
Hemera source pin execution Serves: Kadek foundation row 4 · Radio/soft3 · dependency delivery prerequisite. Parent Radio `a11d342594949d4ea542b26366f71d56ef21662d`; Hemera `d5f0a09539360934fb32db9b2e15a91995607db6`. Original Grok PLAN approved at 2026-10-11T20:36:14Z; the separately reviewed…
soft3/radio/.claude/tasks/bao-root-policy/execution
Execution notes Approved plan v3 remains frozen. Fresh Opus source-only PLAN review: `/tmp/kadek-bao-root-policy-plan-v3-review.txt`, explicit APPROVE, session99218 exit0 (root receipt). Implementation starts from radio258724bd with exact Hemera23f3bbcf; no commit or release is part of this…
soft3/radio/.claude/tasks/bao-empty-status/execution
Current-origin amendment execution Radio `bc8b6331d3c76cbc011210e13f81e6ea351ef06f`, Hemera `d5f0a09539360934fb32db9b2e15a91995607db6`; previous Radio344 candidate and audit remain untouched. At this initial research point production had no changes in the owned worktree. Fresh raw origins,…

Graph