# Bare-metal C LLM engine for a 2006 PS3 (150.7 tok/s, 137x llama.cpp)

> Source: <https://github.com/veniallc/cell-llm>
> Published: 2026-12-03 20:25:15+00:00

**Recommendation:** 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.
More in the playbook "Inference and self-hosting": <https://wpnews.pro/playbooks/inference-serving>

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).

- **Zero `llama.cpp`, zero CUDA, zero Python runtime, zero ML frameworks**
- **`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
- **`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` )
- **`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`
- Compiles natively on the PS3 in **~2 seconds** (vs ~60 min for upstream`llama.cpp` )

Hardware: **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).
Model: `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).

| 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` | 
|---|---|---|---|---|---|
| **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×`** | 
| **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×`** | 
| **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×`** | 
| **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×`** | 
| **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 | 
| **Context / KV Cache** | fp16 KV | fp32 KV | `256` tok (`11.8 MiB` fp32) | **`512` tok (`7.03 MiB` Q8)** | `2×` ctx /`40%` less RAM | 
| **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** | 
| **Native PS3 Compile** | `~60 min` | `~1.4 s` | `~2.0 s` | **`~2.5 s`** | **`~1,440×` faster** | 

**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`.*
***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`).*

### Per-Token Millisecond Breakdown (`v6`, `--profile` at `147.3 tok/s` / `6.79 ms/tok`; `6.63 ms/tok` unprofiled)

- **30-Layer Q4×Q11 Integer MatVec (`QKV`, `W_o`, fused `gate+up+SwiGLU`, `W_down`):**` 4.89 ms`
- **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)
- **6-SPE GQA-Paired Q8 FlashAttention-2 (GQA `9/3`, `head_dim=64`):**` 0.45 ms`
- **Standalone SwiGLU Dispatch:****`0.00 ms`** (100% fused into` CELL_CMD_GATE_UP_SWIGLU` )
- **PowerPC AltiVec RoPE + Fused Residual `add_rmsnorm` (PPE):**`0.30 ms` (`0.09 ms` RoPE +`0.21 ms` norm)
- **6-SPE Hardware Decrementer Balance (`79.8 MHz`, LM head):**` 81,332` –`81,595` ticks (**`0.09%` spread** across`SPE0` –`SPE5` )

1. **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).
  - 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.
2. Packs 4 output rows × 32 input columns into an 
3. **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` ).
  - Uses 8 dual `int32` accumulators (`spu_mule` /`spu_mulo` /`spu_mhhadd` /`spu_madd` ) to completely hide the SPU's 7-cycle integer multiply latency.
4. Quantizes the activation vector 
5. **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`** .
  - 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).
6. Clamps each SPE's tile batch size to a multiple of 4 (
7. **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.
  - 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.
8. Computes 
9. **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.
  - PowerPC AltiVec (`<altivec.h>` ) vectorized`rmsnorm` , fused residual`add_rmsnorm` (for both`W_o` and`W_down` ), and vectorized`apply_rope` .
  - 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).
10. Persistent SPE contexts dispatched via direct 

Download `model.safetensors` and `tokenizer.json` from `HuggingFaceTB/SmolLM2-135M-Instruct`, then run:

```
make convert_cellq4
./convert_cellq4 weights/model.safetensors weights/smollm2-135m-cellq4.cbin
python3 tools/export_tokenizer.py weights/tokenizer.json weights/smollm2.ctok
make SPU_CC=/path/to/spu-gcc SPU_SIZE=/path/to/spu-size \
     LIBSPE2_INC=/path/to/libspe2/include LIBSPE2_LIB=/path/to/libspe2/lib
# Single prompt or multi-run benchmark with optional per-stage profiling (6x Cell SPE)
./cell-llm --model weights/smollm2-135m-cellq4.cbin \
           --tok weights/smollm2.ctok \
           --spu spu/cell_llm_spu.elf \
           --bench 7 --profile \
           --prompt "Explain in two short sentences what a GPU is."

# Sticky nvtop-style terminal HUD chat
./cell-llm --model weights/smollm2-135m-cellq4.cbin \
           --tok weights/smollm2.ctok \
           --spu spu/cell_llm_spu.elf --chat

# Embedded Web Chat + OpenAI /v1/chat/completions SSE server
./cell-llm --model weights/smollm2-135m-cellq4.cbin \
           --tok weights/smollm2.ctok \
           --spu spu/cell_llm_spu.elf --serve 8080

# PPE -O3 CPU-only baseline comparison (same weights)
./cell-llm --model weights/smollm2-135m-cellq4.cbin \
           --tok weights/smollm2.ctok --ppe-only \
           --prompt "Explain in two short sentences what a GPU is."
```

MIT — Venia LLC (`@veniallc`)
