interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / docs / dev-model-0 / DEV_MODEL_0_CODE_AUDIT.md

157 lines · 24562 bytes Raw Blame History Source

DEV-MODEL-0 Phase A: Live Code Audit

Repo: aien-spark/aien-sovereign-core. Audited commit: 7011f2be58a1a08e4774d79ae28db27ad5d17624 (main HEAD on 2026-10-01, unchanged from the brief). Read-only: no builds, no tests were run. All line numbers refer to that SHA. Paths are relative to the repo root; abi = crates/aien-inference-abi/src, rt = crates/aien-inference-runtime/src. External review: Gemini (agy, plan mode); Codex was out of usage. Its citation corrections (weight cache, MaxServingBackend stub, extra template copies, line shifts) are folded in. Classes: GENERIC (reuse as is), GENERALIZABLE (right idea, hard-coded values to lift), MODEL-SPECIFIC (belongs to one family), LEGACY (remove or quarantine).

1. Verdict on the brief (sections 1 and 2)

ClaimResultEvidence
Files in section 1 existCONFIRMED (all 11 plus runtime model.rs)abi/{capsule,checkpoint,tensor_abi,backend,blackwell_backend,transformer_backend,tokenizer,qwen3_coder,qwen3_moe,qwen3_serve}.rs
ModelCapsule, CapsuleTensor, WeightDType, QuantizationDescriptor existCONFIRMEDabi/capsule.rs:13,57,68,79
"Strict Safetensors ingest"PARTLY. Strict for TinyLlama only: BF16-only, exact catalog, one file. Extra tensors in the file are silently ignored (loop iterates the catalog, not the header).abi/checkpoint.rs:233-384, dtype gate :298
"Resident Qwen3 GPU-weight serving"CONFIRMED, but it is a Mojo shared library for ONE Qwen3-Coder-30B-A3B shape with compile-time constantsabi/qwen3_serve.rs:1-5,43-60, abi/qwen3_coder.rs:23-32
2.1 model.rs is TinyLlama-orientedCONFIRMEDrt/model.rs:3,22,74,134,155,207-237
2.2 tokenizer.rs embeds TinyLlama ids, context, template, stopsCONFIRMEDabi/tokenizer.rs:43-49,70-88,106,143
2.3 ModelCapsule is the format; no competing oneCONFIRMED, with a caveat: the capsule is single-file (bytes: Arc<[u8]> is the whole file). Gemma 4 12B ships as ONE 23.9 GB file (see MODEL_DISCOVERY), so single file works, but whole-file std::fs::read into RAM is the current loader (abi/checkpoint.rs:392-403). Qwen uses a different, sharded, positional reader (Shard, abi/qwen3_moe.rs:274-370).
2.4 Qwen3 is the closest precedentCONFIRMED, and it is the only model with a sharded reader, resident device weights, QK-norm and per-family constants. It hard-codes every shape as const.abi/qwen3_coder.rs:23-32
Brief section 1 "Blackwell paths"REFINED: two separate GPU stacks exist (see 4). The TinyLlama one is CUDA/cuBLAS built by nvcc; the Qwen one is Mojo.abi/build.rs, abi/cuda/, abi/mojo/

2. Generic infrastructure inventory

ComponentWhereClassNotes
ModelCapsule, CapsuleTensor, digestsabi/capsule.rs:13-229GENERICdtype enum already covers F32/BF16/FP16/FP8/INT8/INT4; QuantizationDescriptor uses String scheme/packing (not an enum), so identity of quantized variants must be added by caller.
Tensor ABI (host/device views, residency)abi/tensor_abi.rs:7-241, C header abi/cuda/tensor_abi.hGENERICMAX_TENSOR_RANK=4; DeviceTensor::upload real only under cfg(has_blackwell_cuda) (:144-161).
Safetensors ingest (catalog validated)abi/checkpoint.rs:233-403GENERALIZABLEValidator logic is generic; tinyllama_catalog() (:192-231) and BF16-only (:298) are not. Needs: catalog supplied by family, dtype set, extra-tensor rejection, overlap check, multi-file.
Sharded safetensors readerabi/qwen3_moe.rs:274-370 (Shard, pub(crate)), index handling :396-420GENERALIZABLEPositional reads, header cap, range checks, index path-escape check. Error type is Qwen3MoeError; make it family-neutral.
TensorBackend trait (CPU oracle)abi/backend.rs:82-278, ReferenceCpuBackend :280-410GENERALIZABLEFixed op set: rmsnorm, rope, matmul, swiglu, gqa/paged attention, logits. No GELU, no QK-norm, no sliding window, no softcap, no partial rotary, no per-layer head dim.
CPU math kernels (f64 accumulate)abi/tensor.rs:10-300GENERALIZABLEGood oracle numerics (f64 sums). rmsnorm is x*scale*w (:78-94), not Gemma (1+w); RoPE is full-dim rotate_half with one theta (:101-154).
Native forward loop (single token, paged KV, batch)abi/transformer_backend.rs:496-668,919-1256GENERALIZABLEOne uniform Llama block per layer; see section 3.
Paged KV cache with COW branchescrates/aien-kv-cache/src/lib.rs (1633 lines), KvPoolConfig :46-53GENERALIZABLEOne num_kv_heads and one head_dim for the whole pool. Gemma 4 12B needs per-layer-type shapes (8 x 256 sliding, 1 x 512 global) and a window-bounded sliding cache. Has dtype enum incl. BF16/FP8/FP4 (:12-26).
Schedulercrates/aien-scheduler/src/{lib,sequence}.rsGENERICNo model strings (grep clean). Builds ScheduledBatch. SamplingParams.top_p exists (abi/lib.rs:170) but is never read by any sampler.
Backend trait AienInferenceBackendabi/lib.rs:234-247GENERICload_model(&ModelConfig), execute_step, manages_kv_cache.
Streamingabi/transformer_backend.rs:313-394 (generate_tokens_streaming), rt/model.rs:138-173GENERICCallback per token, stop list passed in.
Branch/context API (create/fork/decode_branch_step)abi/transformer_backend.rs:184-311GENERICFeeds branch-native runtime.
Samplingabi/tensor.rs:262-307GENERALIZABLEOnly argmax and temperature with a toy LCG. No top-k, top-p, penalties, no logit softcap. Gemma defaults are temp 1.0, top_k 64, top_p 0.95.
Runtime model owner EmbeddedModelrt/model.rs:21-204MODEL-SPECIFIC (TinyLlama)Concrete tokenizer and backend types, no abstraction.
Inference service wrapperrt/service.rs:1-113, crates/aien-inference-service/src/* (small)GENERALIZABLEWraps EmbeddedModel.
Runtime server and chat formatcrates/aien-runtime/src/server.rs:6,29,216,236, control.rs:25-45MODEL-SPECIFICCalls format_tinyllama_chat, stop [TinyLlamaTokenizer::EOS_TOKEN_ID].
Model identityModelConfig.model_id: String (abi/lib.rs:85), capsule digestsGENERALIZABLENo family tag, no tokenizer/template/config digest, no license reference.

3. Hard-coded model assumptions (every one found)

TinyLlama (all LEGACY-or-MODEL-SPECIFIC until a family boundary exists):

Llama-block assumptions baked into the forward path (abi/transformer_backend.rs:507-649, abi/weights.rs:174-275), none of which Gemma 4 satisfies:

Qwen3-Coder (MODEL-SPECIFIC, all const): 48 layers, hidden 2048, 32 Q heads, 4 KV, head_dim 128, vocab 151936, eps 1e-6, theta 1e7 (abi/qwen3_coder.rs:23-32); FP8 block quant FP8_BLOCK; QK-norm per head (qwen3_coder.rs:61-71, rmsnorm_per_head :~290); MoE shapes QWEN3_A3B_* (abi/qwen3_moe.rs:29-31); fixed exported C symbols qwencoder_* (abi/qwen3_serve.rs:63-100). Reusable PATTERN, not code.

Absent everywhere (grep for sliding, softcap, gelu, tie, query_pre, rope_local in abi, cuda, mojo: zero hits): sliding-window attention, soft-capping, GELU, tied embeddings, embedding scaling, per-layer head shapes, partial/proportional RoPE, multiple stop-token sets, text-only guard for a multimodal checkpoint.

4. Device execution boundaries

Path 1, TinyLlama via TensorBackend (abi/blackwell_backend.rs, abi/cuda/blackwell_gemm.cu):

Path 2, Blackwell batch executor (abi/blackwell_batch_executor.rs, crates/aien-inference-abi/cuda/blackwell_layer.cu 1118 lines): resident BF16 weights with fused QKV and gate/up, persistent workspace, KV scatter into the canonical pool, GPU argmax, one fence per step (:1-11). Fully Llama-shaped (blackwell_batch_executor.rs:281-325: single head_dim, kv_dim, intermediate_dim). Also CUDA. It is the closest thing to what Phase I wants, but it cannot express Gemma 4 without new kernels.

Path 3, Qwen3-Coder resident serve (abi/qwen3_serve.rs, abi/mojo/qwencoder_serve/*.mojo, 617+136 lines): one-time upload of all FP8/BF16 weights, then only activation rows cross; CPU does rmsnorm, QK-norm, RoPE, residuals, router; device does fused QKV, o_proj, MoE, KV append, attention, logits (qwen3_serve.rs:1-5,369-499). Device and CPU KV are separate caches with a loud guard against mixing (:424-436). Uses Mojo max.gpu.DeviceContext (qwencoder_serve.mojo:16), loaded with libloading from libqwencoder_serve.so (qwen3_serve.rs:61-70). This is the pattern to copy for Gemma (resident weights, parity-checked device calls, always-available CPU fallback).

Other: MojoGb10Backend (abi/mojo_backend.rs) loads libaien_kernels.so from abi/mojo/lib.mojo (TinyLlama-era generic kernels). MaxServingBackend (abi/lib.rs:308-375) holds a reqwest client for a Modular MAX endpoint, but its execute_step (:337-373) only returns synthetic token ids (151643 + step_id % 100), no network I/O (LEGACY stub; keep out of the Gemma path).

Sampling happens on host after full logits are read back (transformer_backend.rs:331-360, vocab 262144 x f32 = 1 MB per token for Gemma). GPU argmax exists only in the batch executor.

5. Python, Mojo, build, CLI inventory

Python files in repo: 11 (confirmed). None is imported or shelled out to from the inference path.

Mojo kernels present: abi/mojo/lib.mojo (299), abi/mojo/qwen3_moe/{kernels,lib}.mojo, abi/mojo/qwencoder_serve/{qwencoder_kernels,qwencoder_serve,test_kernels}.mojo, crates/spark-max-cabi/mojo/spark_max_bridge.mojo, crates/spark-aegis/mojo/simd_matcher.mojo, imprints/en2-trinity/mojo/en2_kv_adapter.mojo. Built by abi/mojo/build.sh with mojo build --emit shared-lib.

Build: Cargo workspace of 34 members (Cargo.toml:1-37), resolver 2, edition 2021, release profile LTO + panic=abort (:48-52). NO rust-toolchain file; machine has rustc/cargo 1.98.1 aarch64. Cargo.lock is committed (527 packages). No vendor/ directory; ~/.cargo/registry/cache holds 979 crates including tokenizers-0.21.4, onig-6.5.3, rayon, libloading, reqwest. Offline cargo check -p aien-inference-abi -p aien-inference-runtime --offline with AIEN_FORCE_CPU_STUB=1 (skips nvcc, build.rs:14-18) looks feasible; whole-workspace check also needs the sibling ../aien-protocols path dependency (crates/spark-mail-rs/Cargo.toml:26, spark-harvester/Cargo.toml:25-26), which is present next to this worktree. NOT run in Phase A. CI runs fmt, check, clippy -D warnings and test with --test-threads=1 (.github/workflows/ci.yml:39-51).

CLI entry points: aien binary (crates/aien-cli/src/main.rs) only starts a daemon (commands::run_daemon_server, main.rs:44). There is NO aien model import command. Model discovery is resolve_daemon_manifest() scanning $AIEN_MODEL_DIR, ./models, ~/models for any .safetensors (commands.rs:970-1027), then load_production_weights assumes TinyLlama config (:1066) and on ANY load error silently substitutes synthetic reference test weights (:1086-1098; the same silent fallback exists at rt/model.rs:125-136 and in weights.rs:641 for the bytes loader). That silent substitution must go before any new model is wired. Examples for Qwen: abi/examples/qwen3_coder_chat.rs, qwen3_serve_check.rs, qwen3_parity_check.rs, qwen3_noise_floor.rs.

Tests that pin TinyLlama behavior and must keep passing through Phase B: abi/tests/{tinyllama_parity,transformer_parity,paged_transformer_parity,branch_client_harness,blackwell_batch_executor_parity,paged_attention_parity,bf16_paged_attention,qwen3_moe_gpu}.rs, rt/tests/runtime_tests.rs, crates/aien-runtime/tests/runtime_end_to_end_tests.rs. Oracle fixtures: abi/fixtures/tinyllama_oracle.safetensors + manifest.

6. Ten most important findings

  1. No model-family boundary exists; TinyLlama types are concrete in the runtime (rt/model.rs:3,22), runtime server (server.rs:6,216) and CLI (commands.rs:1055-1070).
  2. Tokenizer is a TinyLlama-only struct with associated constants; a second copy of the chat template lives in aien-runtime/src/control.rs.
  3. The forward pass has no way to express Gemma 4: no sliding window, no two head shapes, no GELU, no softcap, no tied head, no embedding scale, no partial/proportional RoPE.
  4. The paged KV pool and the BF16 attention kernel assume one head shape for all layers and head_dim <= 256; Gemma 4 12B global layers are 1 KV head x head_dim 512.
  5. Whole-model FP32 host expansion (about 48 GB for 12B) is the current load path; BF16-resident is needed and only exists on the Qwen path.
  6. The only GPU path for non-MoE models is CUDA/cuBLAS via nvcc (conflicts with the no-CUDA rule); the resident Mojo path exists only for Qwen3-Coder.
  7. Silent synthetic-weights fallback on load failure in three places violates "fail loudly".
  8. Checkpoint ingest is TinyLlama-catalog, BF16-only, whole-file, and ignores extra tensors; the sharded reader is private to Qwen code.
  9. top_p, max_sequence_length, block_size configuration fields are declared but not enforced or used by samplers/forward.
  10. No Python is in the inference path; Python exists as offline oracle (scripts/generate_tinyllama_oracle.py), harnesses, and a MAX plugin. No aien model import CLI exists.

7. Decisions needed before Phase B/F (for the orchestrator)

8. Phase B-I plan (file ownership, order, first PRs)

Ownership (no file in two lanes; new files named; shared files get exactly one owner, others request changes via board):

LaneOwns
A model family / runtimeabi/lib.rs (ModelConfig region only), new abi/model_family.rs, abi/gemma4.rs (contract, Phase D contract struct), rt/model.rs, rt/service.rs, aien-runtime/src/{server,control}.rs (remove template copy by calling B's trait)
B tokenizer / templateabi/tokenizer.rs plus new abi/model_tokenizer.rs, abi/chat_template.rs, abi/tests/tokenizer_*.rs, fixtures abi/fixtures/gemma4_tokenizer/ (artifact digests only)
C importer / catalogabi/checkpoint.rs, abi/capsule.rs, new abi/gemma4_catalog.rs, new abi/safetensors_shard.rs (lifted from qwen3_moe Shard; Qwen file keeps a thin re-export, touched only via C's PR), new aien-cli/src/model_import.rs + the single main.rs subcommand registration, abi/tests/import_*.rs
D CPU referencenew abi/gemma4_cpu.rs, new abi/gemma_ops.rs (GELU-tanh, (1+w) norm, sliding attention, partial RoPE, softcap), abi/weights.rs (additive struct only), abi/tensor.rs untouched (additions go in gemma_ops)
E oracle / parityabi/tests/gemma4_parity_*.rs, abi/fixtures/gemma4/, docs/dev-model-0/DEV_MODEL_0_PARITY_PROTOCOL.md; no Python files
F Blackwellabi/blackwell_*.rs, abi/cuda/, abi/mojo/ (new mojo/gemma4_serve/), new abi/gemma4_serve.rs, crates/aien-kv-cache/src/lib.rs (heterogeneous layers), abi/build.rs
G DevelopmentModel / RSInew crate crates/aien-dev-model (single removable crate), aien-runtime/src/rsi.rs, workspace Cargo.toml member line
H benchmark quiescencenew scripts/dm0-quiesce.sh, docs/dev-model-0/DEV_MODEL_0_BENCHMARK_PROTOCOL.md, hooks into the existing quiet-flag protocol only
I off-rampdocs/dev-model-0/DEV_MODEL_0_OFF_RAMP.md, new scripts/dm0-removal-qualify.sh, new crates/aien-dev-model/tests/removal_*.rs coordinated with G
J review / licensingNOTICE, ATTRIBUTION.md, LICENSES/, docs/dev-model-0/DEV_MODEL_0_LICENSE_AUDIT.md; read-only elsewhere

Shared hot spot: abi/lib.rs (A) re-exports every module; every lane adds a pub mod line there. Rule: new modules are announced on the board, A applies the one-line pub mod edits, or each lane's PR touches only its own pub mod line and merges serially.

Order and parallelism:

  1. Start now, independent: B (trait + TinyLlama impl), C (catalog + shard reader), J, H (design), I (design), G (interface only).
  2. A starts with B's trait merged (needs ModelTokenizer type), runs in parallel with C.
  3. D starts when A's contract struct and C's catalog types are merged.
  4. E starts when D has single-token output and the external oracle artifacts exist (D2).
  5. F starts after DM4 (CPU forward) passes; kernels can be prototyped in parallel against E's intermediates.
  6. G integrates after DM8 interface can run on CPU; H and I proceed in parallel with F using a stub model.

First three PR-sized steps:

  1. B: introduce trait ModelTokenizer (encode, decode, format_chat, is_stop_token, stop_token_ids, bos/eos, vocab_size, max_context, digests) and make TinyLlamaTokenizer implement it with identical behavior; delete the duplicate template in aien-runtime/control.rs by routing through the trait. Evidence: existing tokenizer tests plus runtime_end_to_end_tests unchanged.
  2. A: add ModelFamily enum and ModelDescriptor (config, tensor catalog fn, tokenizer ctor, stop tokens, context rules); make EmbeddedModel hold Box<dyn ModelTokenizer> and take a descriptor; remove tinyllama_1_1b() calls and the hard-coded paths from rt/model.rs; replace the three silent synthetic-weight fallbacks with explicit errors (keep a clearly named test-only constructor). Evidence: tinyllama_parity, runtime_tests pass unchanged.
  3. C: lift Shard into a neutral safetensors_shard.rs, add a catalog-from-config generator (pure function from a Gemma4 config.json to the expected tensor list) with negative tests (extra tensor, wrong dtype, overflow offsets, missing tensor), no weights needed. Evidence: unit tests on synthetic safetensors.

No production code was changed in Phase A. Gate DM0_AUDIT_PASS is claimed only when this document is merged.