{"slug": "bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp", "title": "Bare-metal C LLM engine for a 2006 PS3 (150.7 tok/s, 137x llama.cpp)", "summary": "A from-scratch pure-C inference engine for the 2006 Sony PlayStation 3 reached 150.72 tokens per second decoding SmolLM2-135M-Instruct (Q4), 137.0× faster than a tuned 2-thread build of upstream llama.cpp (1.10 tok/s) on the same console, the project reports. The engine, cell-llm v6, uses the Cell Broadband Engine's 6 Synergistic Processing Elements with integer mpya instructions and 128-byte DMA, sustaining 17.36 GB/s on the LM head with 0.09% imbalance across SPEs, and prefills 41 tokens at 185.8 tok/s versus llama.cpp's 29.45 seconds. It runs with zero llama.cpp, CUDA, Python runtime or ML frameworks, fits in 24.7 KiB of SPU .text (versus 12 MB stripped llama.cpp), and compiles natively on the PS3 in about 2.5 seconds against roughly 60 minutes for upstream llama.cpp.", "body_md": "Bare-metal-style LLM inference engine written from scratch in pure C + Cell SPU intrinsics for a **2006 Sony PlayStation 3** (Cell Broadband Engine: 1 PowerPC PPE + 6 Synergistic Processing Elements, 215 MiB usable XDR DRAM).\n\n- **Zero `llama.cpp`, zero CUDA, zero Python runtime, zero ML frameworks**\n- **`150.72 tok/s`** peak decode (` 139.5–150.7 tok/s` across`--bench 7` runs) and**`185.8 tok/s`** prefill running` SmolLM2-135M-Instruct` (Q4) 100% resident in PS3 XDR DRAM\n- **`137.0×` faster** than tuned 2-thread upstream`llama.cpp` (`1.10 tok/s` ) on the exact same PlayStation 3 —**`655×` faster** than default`llama.cpp` (`0.23 tok/s` ), and**`162×` faster** than the PS3 PPE`-O3` baseline (`0.93 tok/s` )\n- **`17.36 GB/s` sustained LM-head XDR DRAM bandwidth** (`0.09%` imbalance across all 6 SPEs) with**`25,288 bytes` (`24.7 KiB`)** of SPU`.text` machine code (`156.8 KiB / 256 KiB` Local Store) vs`12 MB` stripped`llama.cpp`\n- Compiles natively on the PS3 in **~2 seconds** (vs ~60 min for upstream`llama.cpp` )\n\nHardware: **2006 Sony PlayStation 3** (`Linux 6.12.62 ppc64`, Cell Broadband Engine `3.192 GHz`, 6 active SPUs, `79.8 MHz` decrementer, `215 MiB` visible XDR DRAM).\nModel: `HuggingFaceTB/SmolLM2-135M-Instruct` (30 layers, `dim=576`, `hidden=1536`, GQA `9/3`, `vocab=49,152`, 4-bit block-32 quantization, `MAX_SEQ=512` Q8 KV cache).\n\n| Metric | Upstream `llama.cpp` (`ee46715` , 2-thr best) | `cell-llm` (PPE`-O3` baseline) | `cell-llm v5` (6× SPE fp32) | **`cell-llm v6` (6× SPE int `mpya` + 128B DMA)** | Speedup vs `llama.cpp` | \n|---|---|---|---|---|---|\n| **Peak Decode Speed** | `1.10 tok/s` (`908.9 ms/tok` )* | `0.93 tok/s` (`1,074 ms/tok` ) | `105.6 tok/s` (`9.5 ms/tok` ) | **`150.72 tok/s`** (` 6.63 ms/tok` ) | **`137.0×`** | \n| **Sustained Mean (`--bench 7`)** | `1.10 tok/s` | `0.93 tok/s` | `94.8–101.3 tok/s` | **`139.5–139.6 tok/s`** (` 150.6` peak)** | **`126.9×`** | \n| **Prefill (41 tokens)** | `29.45 s` (`1.39 tok/s` ) | `37.2 s` (`1.1 tok/s` ) | `0.40 s` (`101.7 tok/s` ) | **`0.22 s` (`185.8 tok/s`)** | **` 133.7×`** | \n| **Model Load Time** | `33.36 s` (BE GGUF) | `~6 s` | `5.3–8.3 s` (`mlock()` ) | **`5.3–8.3 s`** (` mlock()` ) | **`4.0× – 6.3×`** | \n| **Weight File Size** | `85.93 MiB` (`Q4_0` BE GGUF) | `80.29 MiB` (`CELL-Q4_32` ) | `80.29 MiB` (`CELL-Q4_32` ) | **`80.29 MiB` (`CELL-Q4_32`)** | `1.07×` smaller | \n| **Context / KV Cache** | fp16 KV | fp32 KV | `256` tok (`11.8 MiB` fp32) | **`512` tok (`7.03 MiB` Q8)** | `2×` ctx /`40%` less RAM | \n| **SPU `.text` / Binary** | `12 MB` stripped (`18 MB` raw) | `67 KB` | `12.6 KiB``.text` (`82 KB` total) | **`24.7 KiB` `.text` (`<100 KB` total)** | **`>120×` smaller** | \n| **Native PS3 Compile** | `~60 min` | `~1.4 s` | `~2.0 s` | **`~2.5 s`** | **`~1,440×` faster** | \n\n**Upstream `llama.cpp` compiled natively on the PS3 with `libggml`/` libggml-cpu` at `-O3 -mcpu=cell -mtune=cell` (PowerPC AltiVec quants enabled) and endian-swapped `SmolLM2-135M-Instruct-Q4_0-BE.gguf`. Default `n_threads=4` scored `0.23 tok/s` (`4,380 ms/tok`, `655×` slower than `v6`); `n_threads=1` scored `1.03 tok/s`; `n_threads=2` (matching the PPE's 2 SMT threads) was `llama.cpp`'s best score at `1.10 tok/s`.*\n***Measured with `--bench 7` on the live PS3 desktop (which wakes once per second for the status bar, dipping ~2 of 7 runs to `124–132 tok/s` while clean runs consistently hit `150.2–150.7 tok/s`).*\n\n### Per-Token Millisecond Breakdown (`v6`, `--profile` at `147.3 tok/s` / `6.79 ms/tok`; `6.63 ms/tok` unprofiled)\n\n- **30-Layer Q4×Q11 Integer MatVec (`QKV`, `W_o`, fused `gate+up+SwiGLU`, `W_down`):**` 4.89 ms`\n- **Tied LM Head (`49,152 × 576`) + In-SPE SIMD Argmax + Rep Penalty:**` 1.07 ms` (**`17.36 GB/s`** sustained XDR DRAM across 6 SPEs)\n- **6-SPE GQA-Paired Q8 FlashAttention-2 (GQA `9/3`, `head_dim=64`):**` 0.45 ms`\n- **Standalone SwiGLU Dispatch:****`0.00 ms`** (100% fused into` CELL_CMD_GATE_UP_SWIGLU` )\n- **PowerPC AltiVec RoPE + Fused Residual `add_rmsnorm` (PPE):**`0.30 ms` (`0.09 ms` RoPE +`0.21 ms` norm)\n- **6-SPE Hardware Decrementer Balance (`79.8 MHz`, LM head):**` 81,332` –`81,595` ticks (**`0.09%` spread** across`SPE0` –`SPE5` )\n\n1. **Native Big-Endian `CELL-Q4_32` Weight Format (`tools/convert_cellq4.c`):**  - Packs 4 output rows × 32 input columns into an **80-byte, 16-byte-DMA-aligned tile** (4`fp32` scales + 64 bytes of packed 4-bit nibbles,`5.0` bits/weight).\n  - Zero byte-swapping on big-endian `ppc64` ;`mlock()` pins the entire`80.29 MiB` weight buffer in physical XDR DRAM so Linux never pages out a layer.\n2. Packs 4 output rows × 32 input columns into an \n3. **Integer `Q4 × Q11` SPU `mpya` / `mpyhha` MatVec (`spu/cell_llm_spu.c`):**  - Quantizes the activation vector `x` to 11-bit signed`int16` pairs (`xl = q << 4` matching`pk & 0x0F` ,`xh = q` matching unshifted`pk & 0xF0 = 16 * hi` ), eliminating all`32`` cuflt` instructions and all`4`` rotm` nibble shifts per tile while cutting`shufb` in half (`32 → 16` ).\n  - Uses 8 dual `int32` accumulators (`spu_mule` /`spu_mulo` /`spu_mhhadd` /`spu_madd` ) to completely hide the SPU's 7-cycle integer multiply latency.\n4. Quantizes the activation vector \n5. **128-Byte Cell Cache-Line DMA Alignment + 4-Deep Power-of-2 Ring:**  - Clamps each SPE's tile batch size to a multiple of 4 (`batch_tiles &= ~3u` , e.g.`8 × 1440 = 11,520 B = 90 × 128 B` ), preventing unaligned 16-byte Element Interconnect Bus splits and jumping LM-head XDR bandwidth from`12.7 GB/s` to**`17.36 GB/s`** .\n  - Splits `run_matvec_out` and`run_lm_argmax` with a 4-deep branchless`(cur + 3u) & 3u` DMA ring and single 128B-aligned`mfc_put` writes per slice (eliminating ~900 partial-cacheline false-sharing writes per token).\n6. Clamps each SPE's tile batch size to a multiple of 4 (\n7. **Fused `CELL_CMD_GATE_UP_SWIGLU` + GQA-Paired 8-Bit KV FlashAttention-2:**  - Computes `gate` directly into Local Store, streams`up` , applies 4-wide SIMD`SiLU(gate) * up` in-register, and issues a single`1,024 B`` mfc_put` — eliminating 30 SPE dispatches/token.\n  - Compresses KV cache steps to 80 bytes (`64 × uint8` +`fp32` scale + 12 B pad,`MAX_SEQ=512` ,`7.03 MiB` ), halving KV DMA on 2-head SPEs (`SPE 0, 2, 4` ) via paired integer`Q·K` dot products and shared`V` unpacking.\n8. Computes \n9. **Zero-Syscall MMIO Mailboxes + PowerPC AltiVec PPE (`ppe/cell_llm.c`, `ppe/cell_llm_web.h`):**  - Persistent SPE contexts dispatched via direct `SPE_MAP_PS` memory-mapped`SPU_In_Mbox` /`SPU_Out_Mbox` registers with`lwsync` ordering.\n  - PowerPC AltiVec (`<altivec.h>` ) vectorized`rmsnorm` , fused residual`add_rmsnorm` (for both`W_o` and`W_down` ), and vectorized`apply_rope` .\n  - Built-in `--bench <N>` ,`--profile` ,`--chat` (sticky`nvtop` -style ANSI terminal HUD), and`--serve 8080` (pure-C Web Chat + OpenAI-compatible`POST /v1/chat/completions` SSE server).\n10. Persistent SPE contexts dispatched via direct \n\nDownload `model.safetensors` and `tokenizer.json` from `HuggingFaceTB/SmolLM2-135M-Instruct`, then run:\n\n```\nmake convert_cellq4\n./convert_cellq4 weights/model.safetensors weights/smollm2-135m-cellq4.cbin\npython3 tools/export_tokenizer.py weights/tokenizer.json weights/smollm2.ctok\nmake SPU_CC=/path/to/spu-gcc SPU_SIZE=/path/to/spu-size \\\n     LIBSPE2_INC=/path/to/libspe2/include LIBSPE2_LIB=/path/to/libspe2/lib\n# Single prompt or multi-run benchmark with optional per-stage profiling (6x Cell SPE)\n./cell-llm --model weights/smollm2-135m-cellq4.cbin \\\n           --tok weights/smollm2.ctok \\\n           --spu spu/cell_llm_spu.elf \\\n           --bench 7 --profile \\\n           --prompt \"Explain in two short sentences what a GPU is.\"\n\n# Sticky nvtop-style terminal HUD chat\n./cell-llm --model weights/smollm2-135m-cellq4.cbin \\\n           --tok weights/smollm2.ctok \\\n           --spu spu/cell_llm_spu.elf --chat\n\n# Embedded Web Chat + OpenAI /v1/chat/completions SSE server\n./cell-llm --model weights/smollm2-135m-cellq4.cbin \\\n           --tok weights/smollm2.ctok \\\n           --spu spu/cell_llm_spu.elf --serve 8080\n\n# PPE -O3 CPU-only baseline comparison (same weights)\n./cell-llm --model weights/smollm2-135m-cellq4.cbin \\\n           --tok weights/smollm2.ctok --ppe-only \\\n           --prompt \"Explain in two short sentences what a GPU is.\"\n```\n\nMIT — Venia LLC (`@veniallc`)", "url": "https://wpnews.pro/news/bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp", "canonical_source": "https://github.com/veniallc/cell-llm", "published_at": "2026-12-03 20:25:15+00:00", "updated_at": "2026-12-03 20:43:51.082704+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-infrastructure", "machine-learning"], "entities": ["Sony PlayStation 3", "Cell Broadband Engine", "SmolLM2-135M-Instruct", "llama.cpp", "HuggingFaceTB", "cell-llm"], "also_reported_by": [], "takeaway": "Running 6 SPEs with integer mpya matvecs and 128-byte DMA hit 150.72 tok/s decode on a PS3, 137× a tuned 2-thread llama.cpp at 1.10 tok/s, with the tied 49,152×576 LM head alone sustaining 17.36 GB/s of XDR DRAM bandwidth.", "recommendation": {"advice": "Offload matvecs to all 6 Cell SPEs with integer mpya and 128-byte DMA, and fuse gate+up+SwiGLU into one dispatch to cut per-token time; the source measured 150.72 tok/s versus 1.10 tok/s for 2-thread llama.cpp.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp", "markdown": "https://wpnews.pro/news/bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp.md", "text": "https://wpnews.pro/news/bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp.txt", "jsonld": "https://wpnews.pro/news/bare-metal-c-llm-engine-for-a-2006-ps3-150-7-tok-s-137x-llama-cpp.jsonld"}}