| assets/ | ||
| src/ | ||
| tests/ | ||
| .gitignore | 69 B | |
| Cargo.lock | 60.9 KiB | |
| Cargo.toml | 1.6 KiB | |
| LICENSE | 11.1 KiB | |
| README.md | 5.2 KiB |
README.md

coco
An architecture-agnostic LLM inference + quantization engine, in Rust. Built on candle + Metal.
One engine, not one-file-per-architecture. Every model reduces to a config row plus a single pluggable per-layer seam — so supporting a new architecture is an afternoon, not a rewrite.
Status: early but real. Builds clean, cargo test is green (12/12), and the headline pieces are validated against oracles. It is honest about what it can't do yet (see Limitations). This is a working research engine, not a drop-in llama.cpp replacement.
What works today
- Four architectures, all wired end-to-end through one generic
DenseDecoder:- Qwen2, Qwen3 (QK-norm), Qwen3-MoE (per-layer dense/MoE routing), DeepSeek V2/V3 (MLA latent attention + shared-expert MoE + YaRN RoPE).
- One pluggable seam — the
SequenceMixertrait. Three mixers implement it and dispatch per layer:GqaAttention(full GQA + flash-decode + persistent KV cache),MlaAttention(DeepSeek latent),GatedDeltaNet(linear recurrent). - turboquant — a pure-Rust, data-free 3-bit quantizer (FWHT incoherence rotation + Lloyd-Max codebook + per-block fp16 radius). No Python, no calibration set, no external quant libs.
- Measured on Qwen2.5-1.5B: whole model 2944 → 934 MB (3.15×), projections 5.12×, ~3.125 bpw, relFro 0.184.
- Weights stay GPU-resident and compressed, decompressed on-the-fly inside fused Metal kernels — never materialized back to fp16.
- Output is byte-for-byte identical to the fp16 baseline on the checked cases.
- Fused Metal kernel suite (
qlinear.rs): on-GPU rotation, quantized matvec that reads compressed codes directly (register-blocked variants), fused RMSNorm, and split-K flash-decode with in-kernel KV append.
Validated (reproducible)
- GatedDeltaNet forward matches its reference oracle at max abs err 1.825e-7 (mean 3.5e-8), threshold 1e-4 —
cargo run --release --bin gdn_ref, fixtures intests/fixtures/gdn_oracle/. cargo test --release→ 12 passed / 0 failed (FWHT self-inverse, rotated-fast-path == dense matmul, 3-bit roundtrip, …).cargo build --release→ 0 errors.
Limitations (honest)
- Qwen3.5 is recognized but does not run yet. Its config is parsed and the
GatedDeltaNetmixer is validated in isolation, but the per-layer linear/full-attention dispatch isn't wired intoDecoderLayeryet —build_modelreturns a diagnostic error on purpose rather than building a half-working model. - MLA runs the candle fallback (no fused kernel yet); its parity-vs-reference is not re-verified in this release (needs an external checkpoint).
- Speed wins are short-context only. Decode is bandwidth-bound, so reading fewer weight bytes wins at ctx 0–128; the advantage inverts by ~2K–8K because prefill is still O(seq²).
- The integrated binary ships the 4.x-bpw nibble format by default; the true-3-bit (934 MB) path is self-checked but not yet CLI-default.
- No checkpoint persistence / GGUF interop — models are quantized at load.
Quick start
cargo build --release
# run generation (point --model at a HF model dir with config.json, tokenizer.json, *.safetensors)
cargo run --release --bin coco -- --model ./model --prompt "The capital of France is"
# validate the GatedDeltaNet port against its oracle
cargo run --release --bin gdn_ref
cargo test --release
Layout
| file | what |
|---|---|
src/config.rs | arch registry + config parsing (ARCH_REGISTRY, resolve_arch, build_model dispatch) |
src/model.rs | DenseDecoder, DecoderLayer, the SequenceMixer trait + its 3 impls, MoeFfn |
src/turboquant.rs | the data-free quantizer (rotation + codebook + radius) |
src/qlinear.rs | fused Metal kernels (quantized matvec, flash-decode, rmsnorm) |
src/eval.rs / src/merge.rs | eval harness + evolutionary merge |
tests/fixtures/ | golden oracles for validation |
The story
The design decisions here are written up, for humans, at bitacora.utopiaia.com — the SequenceMixer seam, oracle-first validation, turboquant, the bandwidth thesis, and an honest engineering war story about a GPU kernel that came out 20× slower.
License
Apache-2.0. See LICENSE.