| 1 | <div align="center"> |
| 2 | |
| 3 |  |
| 4 | |
| 5 | # coco |
| 6 | |
| 7 | **An architecture-agnostic LLM inference + quantization engine, in Rust.** Built on [candle](https://github.com/huggingface/candle) + Metal. |
| 8 | |
| 9 |  |
| 10 |  |
| 11 |  |
| 12 |  |
| 13 | |
| 14 | [](https://coco.utopiaia.com) |
| 15 | [](https://bitacora.utopiaia.com/posts/01-un-motor-para-todos.html) |
| 16 | [](https://bitacora.utopiaia.com) |
| 17 | |
| 18 | </div> |
| 19 | |
| 20 | > ⚠️ **Experimental — work in progress.** coco is an early-stage research engine under active development. It builds and the core pieces are validated against oracles, but its APIs, on-disk formats, and coverage are unstable and it is **not production-ready**. Expect rough edges, breaking changes, and gaps. Use it to learn from and hack on, not to ship. |
| 21 | |
| 22 | > 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. |
| 23 | |
| 24 | Status: **experimental, early but real.** It builds clean and the headline pieces are validated against reference oracles. It is honest about what it can't do yet (see *Limitations*). A research engine to learn from — not a drop-in llama.cpp replacement. |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## What works today |
| 29 | |
| 30 | - **Four architectures**, all wired end-to-end through one generic `DenseDecoder`: |
| 31 | - **Qwen2**, **Qwen3** (QK-norm), **Qwen3-MoE** (per-layer dense/MoE routing), **DeepSeek V2/V3** (MLA latent attention + shared-expert MoE + YaRN RoPE). |
| 32 | - **One pluggable seam** — the `SequenceMixer` trait. Three mixers implement it and dispatch per layer: |
| 33 | `GqaAttention` (full GQA + flash-decode + persistent KV cache), `MlaAttention` (DeepSeek latent), `GatedDeltaNet` (linear recurrent). |
| 34 | - **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. |
| 35 | - Measured on Qwen2.5-1.5B: whole model **2944 → 934 MB (3.15×)**, projections **5.12×**, ~3.125 bpw, relFro 0.184. |
| 36 | - Weights stay **GPU-resident and compressed**, decompressed on-the-fly inside fused Metal kernels — never materialized back to fp16. |
| 37 | - Output is **byte-for-byte identical** to the fp16 baseline on the checked cases. |
| 38 | - **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. |
| 39 | |
| 40 | ## Validated (reproducible) |
| 41 | |
| 42 | - **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 in `tests/fixtures/gdn_oracle/`. |
| 43 | - Unit tests cover FWHT self-inverse, rotated-fast-path == dense matmul, and 3-bit roundtrip (`cargo test --release`, all green). |
| 44 | - **turboquant** output is byte-for-byte identical to the fp16 baseline on the checked cases. |
| 45 | - `cargo build --release` → 0 errors. |
| 46 | |
| 47 | ## Limitations (honest) |
| 48 | |
| 49 | - **Qwen3.5 is recognized but does not run yet.** Its config is parsed and the `GatedDeltaNet` mixer is validated in isolation, but the per-layer linear/full-attention dispatch isn't wired into `DecoderLayer` yet — `build_model` returns a **diagnostic error on purpose** rather than building a half-working model. |
| 50 | - **MLA** runs the candle fallback (no fused kernel yet); its parity-vs-reference is not re-verified in this release (needs an external checkpoint). |
| 51 | - **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²). |
| 52 | - 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. |
| 53 | - No checkpoint persistence / GGUF interop — models are quantized at load. |
| 54 | - **Test coverage is thin** (a handful of unit tests). A real suite — target **≥1200 cases** covering integer/float **overflow**, numerical edge cases, and **security/robustness** (malformed inputs, adversarial shapes/dims) — is planned but **not yet written**. Do not assume the engine is hardened. |
| 55 | |
| 56 | ## Quick start |
| 57 | |
| 58 | ```bash |
| 59 | cargo build --release |
| 60 | # run generation (point --model at a HF model dir with config.json, tokenizer.json, *.safetensors) |
| 61 | cargo run --release --bin coco -- --model ./model --prompt "The capital of France is" |
| 62 | # validate the GatedDeltaNet port against its oracle |
| 63 | cargo run --release --bin gdn_ref |
| 64 | cargo test --release |
| 65 | ``` |
| 66 | |
| 67 | ## Layout |
| 68 | |
| 69 | | file | what | |
| 70 | |---|---| |
| 71 | | `src/config.rs` | arch registry + config parsing (`ARCH_REGISTRY`, `resolve_arch`, `build_model` dispatch) | |
| 72 | | `src/model.rs` | `DenseDecoder`, `DecoderLayer`, the `SequenceMixer` trait + its 3 impls, `MoeFfn` | |
| 73 | | `src/turboquant.rs` | the data-free quantizer (rotation + codebook + radius) | |
| 74 | | `src/qlinear.rs` | fused Metal kernels (quantized matvec, flash-decode, rmsnorm) | |
| 75 | | `src/eval.rs` / `src/merge.rs` | eval harness + evolutionary merge | |
| 76 | | `tests/fixtures/` | golden oracles for validation | |
| 77 | |
| 78 | ## The story |
| 79 | |
| 80 | The design decisions here are written up, for humans, on the **[Bitácora](https://bitacora.utopiaia.com)** — with interactive demos. The coco series: |
| 81 | |
| 82 | 1. [Every new AI model breaks the tooling. I built an engine that shrugs it off.](https://bitacora.utopiaia.com/posts/01-un-motor-para-todos.html) — the SequenceMixer seam |
| 83 | 2. [How I ported a new kind of "attention" to Rust without missing a single decimal](https://bitacora.utopiaia.com/posts/02-oracle-first.html) — oracle-first validation |
| 84 | 3. [Shrinking an AI model 3× with no data and no quality loss](https://bitacora.utopiaia.com/posts/03-encoger-sin-datos.html) — turboquant |
| 85 | 4. [Why a 3 GB model runs faster than a 14 GB one (and when it stops)](https://bitacora.utopiaia.com/posts/04-tesis-ancho-de-banda.html) — the bandwidth thesis |
| 86 | 5. [The GPU trick: multiplying straight from compressed data, without unpacking it](https://bitacora.utopiaia.com/posts/05-kernels-comprimidos.html) — fused Metal kernels |
| 87 | 6. [I optimized the GPU and it came out 20× slower. The lesson.](https://bitacora.utopiaia.com/posts/06-megakernel-20x.html) — occupancy over traffic-saved |
| 88 | 7. [When the honest thing is for your program to say "not yet"](https://bitacora.utopiaia.com/posts/07-todavia-no.html) — the diagnostic bail |
| 89 | |
| 90 | ## License |
| 91 | |
| 92 | Apache-2.0. See [LICENSE](LICENSE). |