Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
clang -x c -std=c11 -fsyntax-only -Wall -Wextra -Werror docs/kernel-abi/gcip_uapi.h
clang -x c -std=c11 -fsyntax-only -Wall -Wextra -Werror docs/kernel-abi/edgetpu_buenos_uapi.h
clang -x c -std=c11 -fsyntax-only -Wall -Wextra -Werror docs/kernel-abi/gxp_laguna_uapi.h
clang -x c -std=c11 -fsyntax-only -Wall -Wextra -Werror -Ilibfreetpu/include llama-cpp-edgetpu/openbuenos_shim.c
clang -x c -std=c11 -fsyntax-only -Wall -Wextra -Werror -Ilibfreetpu/include llama-cpp-edgetpu/freetpu_shim.c

- name: Run Python ISA & DGC0 tool self-tests
run: |
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ All hardware metrics below were measured on Google Pixel 10 Pro Fold (`rango`, T
| Proof Suite | Scope | Verified Result |
| --- | --- | --- |
| **DVFS + FW WakeLock Silicon Unlock** | `1x2048x2048` & `64x1536x1536` (`PM=5` @ `1,286.4 MHz` + Litebuf `AcquireWakeLock`) | **`27.59 GB/s`** (`152 us` HW), **`983.6 INT8 GOPS` @ `177.2 mW` = `5.55 TOPS/W`** (`307 us` HW, `max_abs_err = 0`) |
| **Closed-Form Multi-Tile & Chained SwiGLU ISA** | `92` GEMM refs + `16` SwiGLU refs + `6` standalone non-PoT SwiGLU probes (`11` chunks, `sgs=1`) | **`92/92`** GEMM + **`16/16`** SwiGLU refs + **`6/6`** standalone SwiGLU probes bit-exact (`max_abs_err = 0`, up to **`952.6 GOPS`** / **`26.23 GB/s`**); **`20/20`** `libopenbuenos` host suites PASS; **`openbuenos_selftest --test-swiglu` `8/8` PASS live** (`nmse=2.14e-03` on M=64 Q4) |
| **Closed-Form Multi-Tile & Chained SwiGLU ISA** | `92` GEMM refs + `16` SwiGLU refs + `6` standalone non-PoT SwiGLU probes (`11` chunks, `sgs=1`) | **`92/92`** GEMM + **`16/16`** SwiGLU refs + **`6/6`** standalone SwiGLU probes bit-exact (`max_abs_err = 0`, up to **`952.6 GOPS`** / **`26.23 GB/s`**); **`20/20`** `libfreetpu` host suites PASS; **`freetpu_selftest --test-swiglu` `8/8` PASS live** (`nmse=2.14e-03` on M=64 Q4) |
| **`0x640` Minimax Splines & Chained Fusion** | `SiLU`/`GELU`/`exp` + `4` Mode-A (`+0 B`) + `3` multi-op `0xa40` probes (`MLP`, `SwiGLU`, `QKV`) | **`12,288/12,288`** spline bytes + **`3/3`** chained probes (`60.8–72.8 us`, `8/8` `ext[2]` slices bit-exact, `max_abs_err = 0`) |
| **Native PT `W8A16(FP16)` (`FP32` Acc) & Causal Huffman/`W4` Verdict** | `1x512..1x2048` (`--test-w8a16`, `openbuenos_selftest`) + `64x1536` 9-chunk PC compiler probe (FP16 intermediate) + `9` Huffman/`W4` silicon probes | **`8/8` single-row `--test-w8a16` PASS live** (`18.32 GB/s` at `1x2048` `229 us`, `relL2 = 2.13e-4`); separate `64x1536` 9-chunk PC compiler probe reached **`512.7 GOPS`**; causally proved hardware Huffman (`0x0a40 bit499` / `0x0cc0`) and 1:4 `W4` DMA unpack are **inactive on `buenos_a0`** |
| **Native PT `W8A16(FP16)` (`FP32` Acc) & Causal Huffman/`W4` Verdict** | `1x512..1x2048` (`--test-w8a16`, `freetpu_selftest`) + `64x1536` 9-chunk PC compiler probe (FP16 intermediate) + `9` Huffman/`W4` silicon probes | **`8/8` single-row `--test-w8a16` PASS live** (`18.32 GB/s` at `1x2048` `229 us`, `relL2 = 2.13e-4`); separate `64x1536` 9-chunk PC compiler probe reached **`512.7 GOPS`**; causally proved hardware Huffman (`0x0a40 bit499` / `0x0cc0`) and 1:4 `W4` DMA unpack are **inactive on `buenos_a0`** |
| **`ggml-edgetpu` Backend Ops (Dense + MoE)** | Live `rango` `test-backend-ops -b EDGETPU` (`MUL_MAT` + `MUL_MAT_ID` MoE, `Q8_0` & `Q4_0`) | **`435/435` PASS** (`104/104` `MUL_MAT q8_0`, `104/104` `MUL_MAT q4_0`, **`227/227` `MUL_MAT_ID` MoE** = `115/115 q8_0` + `112/112 q4_0`), `cpu_fallback_slices = 0`, `DT_NEEDED = [libdl.so, libm.so, libc.so]` |
| **Live LLM Throughput, Residency & Energy (`llama-bench`)** | `SmolLM2-135M-Q8_0` & `Qwen2.5-1.5B-{Q8_0,Q4_0}` (`PM=5` @ `1,286.4 MHz`, `SPSC async dequant`, `MAPS_GOOGLE=0`) | **`SmolLM2 Q8 pp512 = 1,249.96 t/s`** (`-7.9%` SoC `mJ/tok` vs CPU, `tg64 = 34.23 t/s`); **`Qwen2.5 Q8 pp512 = 179.36 t/s`** (**`-62.7%` SS / `-55.3%` INT `mJ/tok` vs CPU** at `1.38 W`); **`Qwen2.5 Q4 pp512 = 180.37 t/s`** (**beats 4T CPU `160.2–176.3 t/s`**, `-39.8%` `mJ/tok`, warm `tg64 = 10.45 t/s`), `w_evict = 0` |
| **Dual-Pixel Wired TPU Cluster (`2× buenos_a0`)** | `Qwen2.5-32B-Instruct-Q4_0` (`17.43 GiB` > 16 GB) over direct 5 Gbps USB-C NCM (`rango` + `mustang`) | **`18.94 GiB` combined peak RSS** (`9.83 GiB` + `9.11 GiB`, `0 kB` swap on Node 2), `34/65` layers offloaded, `hw_us = 8.17 s` (`99.62%` hit, `cpu_fallback_slices = 0`), **`MAPS_GOOGLE = 0`** on both phones |
Expand All @@ -114,7 +114,7 @@ See [docs/PHASE8_PERF_REPORT.md](docs/PHASE8_PERF_REPORT.md) (and [Phase 7](docs

- **Zero vendor binaries**: No proprietary `.so`, `.ko`, `.img`, `.apk`, `.gguf`, `.tflite`, `.dgc0`, or firmware blobs are included in this repository.
- **Zero vendor userspace dependencies**: `libfreetpu`, `tpu_baremetal_runner`, and `ggml-edgetpu` talk directly to the Linux kernel (`/dev/edgetpu-soc` and `/dev/dma_heap/system`).
- **Host mock mode**: Setting `ob_config_t::mock_mode = 1` (or `OPENBUENOS_MOCK=1`) runs the full buffer pool, graph LRU cache, 16-credit async ring, VLIW emitter, and quantized math suites offline on macOS/Linux without TPU hardware.
- **Host mock mode**: Setting `freetpu_config_t::mock_mode = 1` (or `FREETPU_MOCK=1`) runs the full buffer pool, graph LRU cache, 16-credit async ring, VLIW emitter, and quantized math suites offline on macOS/Linux without TPU hardware.

### Extracting `dive_interp_rv64.bin` On-Device

Expand All @@ -125,15 +125,15 @@ Running synthesized `.dgc0` microkernels on physical Tensor G5 hardware requires
#include <stdlib.h>

size_t interp_size = 0;
ob_gemm_extract_dive_interp_from_dgc0("/path/to/local_compiled.dgc0", NULL, 0, &interp_size);
freetpu_gemm_extract_dive_interp_from_dgc0("/path/to/local_compiled.dgc0", NULL, 0, &interp_size);
uint8_t *buf = (uint8_t *)malloc(interp_size);
ob_gemm_extract_dive_interp_from_dgc0("/path/to/local_compiled.dgc0", buf, interp_size, &interp_size);
freetpu_gemm_extract_dive_interp_from_dgc0("/path/to/local_compiled.dgc0", buf, interp_size, &interp_size);
```

Or point `libfreetpu` / `ggml-edgetpu` directly at your local extracted image (or a local `.dgc0` file) at runtime:

```bash
export OPENBUENOS_DIVE_INTERP_PATH=/data/local/tmp/tpu_open_stack/dive_interp_rv64.bin
export FREETPU_DIVE_INTERP_PATH=/data/local/tmp/tpu_open_stack/dive_interp_rv64.bin
```

---
Expand Down
30 changes: 15 additions & 15 deletions docs/PHASE5_LLAMA_TPU_SPEC.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

---

## 1. Clean-Room C++17 `libopenbuenos` GEMM Synthesizer & Runtime
## 1. Clean-Room C++17 `libfreetpu` GEMM Synthesizer & Runtime

Implemented strictly inside [`libopenbuenos/`](../libopenbuenos/):
Implemented strictly inside [`libfreetpu/`](../libfreetpu/):

- **Public API** ([`openbuenos.h`](../libopenbuenos/include/openbuenos.h)):
- `ob_gemm_register_builtin_emitter(ob_context_t* ctx, const char* dive_interp_path)` loads the external RV64GCV DIVE interpreter binary (`ext[1]`, `638,977 B`) at runtime via `dive_interp_path`, `OPENBUENOS_DIVE_INTERP_PATH`, or default `/data/local/tmp/tpu_open_stack/dive_interp_rv64.bin` (or extracts `ext[1]` via `ob_gemm_extract_dive_interp_from_dgc0`).
- Added `OB_ACT_SILU = 4u` and `OB_ACT_GELU = 5u` in `enum ob_activation` for Mode-A zero-overhead (`+0 B`) activation fusion.
- **Native FlatBuffer + VLIW Synthesizer** ([`ob_gemm_emitter.cpp`](../libopenbuenos/src/ob_gemm_emitter.cpp)):
- **Public API** ([`freetpu.h`](../libfreetpu/include/freetpu.h)):
- `freetpu_gemm_register_builtin_emitter(freetpu_context_t* ctx, const char* dive_interp_path)` loads the external RV64GCV DIVE interpreter binary (`ext[1]`, `638,977 B`) at runtime via `dive_interp_path`, `FREETPU_DIVE_INTERP_PATH`, or default `/data/local/tmp/tpu_open_stack/dive_interp_rv64.bin` (or extracts `ext[1]` via `freetpu_gemm_extract_dive_interp_from_dgc0`).
- Added `FREETPU_ACT_SILU = 4u` and `FREETPU_ACT_GELU = 5u` in `enum freetpu_activation` for Mode-A zero-overhead (`+0 B`) activation fusion.
- **Native FlatBuffer + VLIW Synthesizer** ([`freetpu_gemm_emitter.cpp`](../libfreetpu/src/freetpu_gemm_emitter.cpp)):
- Clean-room C++17 port of [`tools/multitile_synth.py`](../tools/multitile_synth.py) + [`tools/dgc0_builder.py`](../tools/dgc0_builder.py) (`DIVE ext[0]`, `DGD0`, `MultiExecutable`, `DWS0`, `DGC0`), compiled with `-std=c++17 -O2 -Wall -Wextra -Werror -fno-exceptions -fno-rtti`.
- **87/87 bit-exact parity** verified offline (`test_builtin_emitter_81_parity` `81/81` + `test_lt_1x_6_parity` `6/6` against reference containers across `c0..c6`, `ext[0]`, `ext[2]`, and `parse_dgc0_buffer`).
- **Closed-Form Hardware-Looped `M=1` `lt_1x*` Large-Tile ISA** ([`ob_gemm_emitter.cpp`](../libopenbuenos/src/ob_gemm_emitter.cpp), [`ob_gemm.cpp`](../libopenbuenos/src/ob_gemm.cpp)):
- **Closed-Form Hardware-Looped `M=1` `lt_1x*` Large-Tile ISA** ([`freetpu_gemm_emitter.cpp`](../libfreetpu/src/freetpu_gemm_emitter.cpp), [`freetpu_gemm.cpp`](../libfreetpu/src/freetpu_gemm.cpp)):
- Single-dispatch schedules for `M = 1`, `K, N in {512, 576, 1024, 1536, 2048}` and cross-products (`1x576x1536`, `1x1536x1536`, `1x2048x2048`, `1x1536x8960` in `5` tiles, etc.).
- Reduces VII dispatches per token on `M=1` decode by **20.9x** on `SmolLM2-135M` (`4,902 -> 234` calls) and **39.6x** on `Qwen2.5-1.5B` (`24,057 -> 607` calls).
- For `M > 1`, symmetric tiles pad `tm in [2..7] -> 8` and `[9..15] -> 16` (`hw_m in {1, 8, 16}`), eliminating the hardware `(M in {2,4}, K=64)` mismatch observed on `rango`.
Expand All @@ -27,23 +27,23 @@ Implemented strictly inside [`libopenbuenos/`](../libopenbuenos/):
### 2.1 Breaking the 1,024-Handle Firmware Ceiling (`Probe A` Dynamic `param_slice`)
- Live instrumentation proved `buenos` firmware enforces a ~1,024 concurrent `RegisterModel` cap (auto-eviction plateaued at `769` live graphs, consuming `141 MiB` in per-graph `scratch_buf`).
- `--probe-dyn-weights` proved on silicon that `Execute` accepts swappable `param_slice` weight DMA-BUFs on a single shape handle (`probe_A_exec_param_swap`: `max_err=0`; `probe_A2_async16_param_swap`: **16 concurrent in-flight swaps on 1 `model_handle`**, `max_err=0`).
- [`ob_graph.cpp`](../libopenbuenos/src/ob_graph.cpp) and [`ob_gemm.cpp`](../libopenbuenos/src/ob_gemm.cpp) key `CachedGraph` handles purely by `(hw_m, hw_k, hw_n, zp_a, zp_b, scale_d, act)` (`34–68` handles total for `SmolLM2-135M` and `Qwen2.5-1.5B`, **0 evictions**, **`99.61%–99.98%` hit rate**), map `image_buf`/`vm_ir_buf`/`params_buf`/`reg_cmd_buf`/`exec_cmd_buf` as `OB_BUF_BIDIRECTIONAL` (eliminating RV64 `.bss` `0x9ac00..0x9c000` IOMMU write faults), and share one read-only-semantics `ext[1]` DMA-BUF across graphs.
- [`freetpu_graph.cpp`](../libfreetpu/src/freetpu_graph.cpp) and [`freetpu_gemm.cpp`](../libfreetpu/src/freetpu_gemm.cpp) key `CachedGraph` handles purely by `(hw_m, hw_k, hw_n, zp_a, zp_b, scale_d, act)` (`34–68` handles total for `SmolLM2-135M` and `Qwen2.5-1.5B`, **0 evictions**, **`99.61%–99.98%` hit rate**), map `image_buf`/`vm_ir_buf`/`params_buf`/`reg_cmd_buf`/`exec_cmd_buf` as `FREETPU_BUF_BIDIRECTIONAL` (eliminating RV64 `.bss` `0x9ac00..0x9c000` IOMMU write faults), and share one read-only-semantics `ext[1]` DMA-BUF across graphs.

### 2.2 Persistent `O(1)` Strided Content-Hash Weight Cache
- Full-byte hashing of `256 KiB–4 MiB` `lt_1x*` weight tiles consumed `~7.6 ms` CPU per call (`CPU=1,231 mW`).
- Replaced with a pure `O(1)` strided content fingerprint (`hash_weight_tile_fast`, touching `~1.1 KiB` on tiles `> 16 KiB` with zero pointer dependence — safe for `Q4_0` reused scratch buffers and `test-backend-ops`).

### 2.3 Activation Underflow Cure (Qwen2.5 Super-Activations)
- A 28-layer X-ray on `Qwen2.5-1.5B-Q8_0` showed attention-sink outliers (`x_max / rms = 36.7`) crushed bulk activations to 2 bits under static uniform `D` calibration.
- Fixed in `ob_gemm.cpp` with: (a) sparse activation outlier peel (`<= 8` channels when `|x| > 8 * rem_rms`, async-overlapped FP32 residual, `0` CPU slice fallbacks), (b) power-of-two RMS `D`-downshift (`shift in [0..4]`), and (c) runtime saturation retry in `drain_one` (`abs(q_out) >= 127` re-dispatches that tile on TPU with scaled `D`).
- Fixed in `freetpu_gemm.cpp` with: (a) sparse activation outlier peel (`<= 8` channels when `|x| > 8 * rem_rms`, async-overlapped FP32 residual, `0` CPU slice fallbacks), (b) power-of-two RMS `D`-downshift (`shift in [0..4]`), and (c) runtime saturation retry in `drain_one` (`abs(q_out) >= 127` re-dispatches that tile on TPU with scaled `D`).
- Result on `Qwen2.5-1.5B-Q8_0` (`-p "The capital of France is"`): **15/15 exact token match to CPU**.

---

## 3. `ggml-edgetpu` Backend & Step 3/4 Hardware Findings

- [`ggml-edgetpu.cpp`](../llama-cpp-edgetpu/ggml-edgetpu.cpp):
- Default-enables `ob_gemm_register_builtin_emitter` when `OPENBUENOS_MOCK=0` (fails loudly if interpreter missing; zero silent CPU fallback), `graph_cache_capacity = 2048` (`OPENBUENOS_CACHE_CAP`), `buffer_pool_max_bytes = 128 MiB`, and supports both `GGML_TYPE_Q8_0` and `GGML_TYPE_Q4_0`.
- Default-enables `freetpu_gemm_register_builtin_emitter` when `FREETPU_MOCK=0` (fails loudly if interpreter missing; zero silent CPU fallback), `graph_cache_capacity = 2048` (`FREETPU_CACHE_CAP`), `buffer_pool_max_bytes = 128 MiB`, and supports both `GGML_TYPE_Q8_0` and `GGML_TYPE_Q4_0`.
- **Step 3 — Tensor G5 (`buenos_a0`) Subbyte INT4 Hardware Verdict & `W8A16(BF16)` Unlock** ([`int4_and_fusion_spec.md`](isa-gemm/int4_and_fusion_spec.md)):
- Live `b2b` probe (`hw_us=398`, `16/16` exact `max_abs_err=0`) proved `0x980 w8` encodes `1:2 UINT8 -> BFLOAT16` (`BF16(float(u8 - zp))`), **not** 4-bit nibble unpacking; `buenos_a0` has no native W4 DMA nibble-unpack engine (`CSR 0x56[6:4]=7` saturates).
- Solving the `1024 B` -> `2048 B` feeding a `4096 B` BF16 MAC deadlock unlocked native **`W8A16(BF16)` FP32-I/O GEMM** (`probe_deq_b4a/b_u8_2048B`: **`32/32` exact FP32, `max_f32_err=0.0`, `hw_us=411–426`**).
Expand All @@ -55,16 +55,16 @@ Implemented strictly inside [`libopenbuenos/`](../libopenbuenos/):
## 4. Live Silicon Verification & Benchmark Matrix (`rango`)

### 4.1 Unit, Host, ELF & Kernel Verification
- **Host suite** (`make -C libopenbuenos verify`): **14/14 suites PASS** (expanded to 16/16 in Phase 6), including `87/87` bit-exact container parity (`81/81` + `6/6 lt_1x*`), `4/4` Mode-A (`176/176` exact), `NMSE < 1e-3`, and `verify-android` (`0` Google `.so`, `0` `dlopen`/`dlsym`).
- **Host `llama-cpp-edgetpu`** (`OPENBUENOS_MOCK=1 test-backend-ops -b EDGETPU -o MUL_MAT`): **327/327 PASS**.
- **Stripped ARM64 ELFs** (`llvm-readelf -d`): ONLY `[libdl.so, libm.so, libc.so]` on `llama-cli`, `llama-bench`, `llama-completion`, `test-backend-ops`, and `openbuenos_selftest`.
- **Live `rango` `openbuenos_selftest` (Phase 5B baseline, `38.4 MHz` prior to Phase 6 DVFS unlock)**:
- **Host suite** (`make -C libfreetpu verify`): **14/14 suites PASS** (expanded to 16/16 in Phase 6), including `87/87` bit-exact container parity (`81/81` + `6/6 lt_1x*`), `4/4` Mode-A (`176/176` exact), `NMSE < 1e-3`, and `verify-android` (`0` Google `.so`, `0` `dlopen`/`dlsym`).
- **Host `llama-cpp-edgetpu`** (`FREETPU_MOCK=1 test-backend-ops -b EDGETPU -o MUL_MAT`): **327/327 PASS**.
- **Stripped ARM64 ELFs** (`llvm-readelf -d`): ONLY `[libdl.so, libm.so, libc.so]` on `llama-cli`, `llama-bench`, `llama-completion`, `test-backend-ops`, and `freetpu_selftest`.
- **Live `rango` `freetpu_selftest` (Phase 5B baseline, `38.4 MHz` prior to Phase 6 DVFS unlock)**:
- `1x576x1536`: `tiles=1, hw_us=1282, wall_us=3064, max_abs_err=0, nmse=1.50e-4`
- `1x2048x2048`: `tiles=1, hw_us=4108, wall_us=5869, max_abs_err=0, nmse=1.43e-4`
- `1x1536x8960`: `tiles=5, hw_us=14335, wall_us=18170, max_abs_err=0, nmse=1.39e-4`
- `outlier_spike`: `40sigma` peel (`hw_us=1253, nmse=5.56e-4, cpu_fb=0`), `12-spike` `D*4` retry (`hw_us=2598, nmse=2.92e-5, cpu_fb=0`)
- `fused_act`: `silu/gelu` on `1x16x16` (`654/604 us`) and `1x576x1536` (`1737/1811 us`), all `max_abs_err=0`
- **Live `rango` `test-backend-ops -b EDGETPU -o MUL_MAT` (`OPENBUENOS_MOCK=0`)**:
- **Live `rango` `test-backend-ops -b EDGETPU -o MUL_MAT` (`FREETPU_MOCK=0`)**:
- `type_a=q8_0,type_b=f32`: **104/104 supported ops PASS** (`1` non-contiguous permuted case `[0,2,1,3]` intentionally unsupported), `cpu_fallback_slices=0`, `tiles=64,011`, `hw_us=43.68 s`, `32.0°C`, ODPM `TPU=171.6 mW`, `MAPS_GOOGLE=0`.
- `type_a=q4_0,type_b=f32`: **104/104 supported ops PASS**, `cpu_fallback_slices=0`, `tiles=124,556`, `cache_entries=27`, `cache_evictions=0`, `cache_hit_rate=99.98%`, `hw_us=73.57 s`, `34.0°C`, ODPM `TPU=40.7 mW`, `MAPS_GOOGLE=0`.
- **Live Single-Dispatch Large-Tile Sweep**: **9/9** `max_abs_err=0`, `1` VII call each, up to `1x4096x4096` (`16 MiB`) and `64x576x1536` (`4.49 ms hw`, **`12.614 GMAC/s`**).
Expand Down
Loading
Loading