FreeTPU: Clean-room reverse engineering of Pixel Tensor G5 into a bare-metal TPU & llama.cpp runtime
Clean-room teardown and replacement of Pixel Tensor G5 EdgeTPU (buenos_a0): bypassing Android's AIDL/NNAPI allowlist, synthesizing multi-tile VLIW GEMMs (81/81 bit-exact), driving /dev/edgetpu-soc over Litebuf VII with zero Google .so dependencies, solving graph-handle thrashing via dynamic param_slice swapping for live llama.cpp LLM inference, and diagnosing LiteRT#12178 to unlock native W8A16 (BF16) FP32 GEMM.
- DGC0 + VLIW ISA compile-diff recovery
- Litebuf VII + libopenbuenos zero-.so runtime
- Dynamic param_slice LLM + W8A16 (BF16) GEMM
Investigation question
How was Google's closed Tensor G5 (buenos_a0) EdgeTPU stack clean-room reverse engineered from a public Pixel factory image and AOSP kernel interfaces into a zero-vendor-library bare-metal VLIW runtime, Litebuf VII kernel transport, and llama.cpp backend capable of running SmolLM2-135M and Qwen2.5-1.5B on live silicon?
Observed
1. Clean-Room Starting Boundary: Beginning strictly from the public Pixel 10 Pro Fold (CP3A.261005.005) factory image, public AOSP edgetpu/gxp kernel driver interfaces, and an official bootloader unlock, binary inspection showed unprivileged callers hitting UID-gated allowlist checks (status code 16 unauthenticated, EINVAL from GetEdgeTpuFd) across Java, NNAPI, and AIDL binder services (IEdgeTpuAppService V10 and IEdgeTpuVendorService V2), while /vendor/etc/edgetpu/custom_kernel.pbtxt is an orphaned host test artifact compiled out on device. 2. Cracking the Closed VLIW ISA via Differential Compile-Diff-Verify: Running 150+ differential micro-TFLite models through live silicon and dissecting DGC0 containers decoded 77 DMA/TTU binaries with 0 failures (184/184 32-bit 0xa40 DMA descriptors, 74/74 0x9c0 tails, 110/110 0x940 and 338/338 0x980 SRAM ring descriptors), uncovered the 638,977 B 64-bit RISC-V (RV64GCV SiFive X280) DIVE VM interpreter in ext[1], mapped the 98 x 25-bit shadow-CSR bank (0x00..0x61) starting at bit 77 of HDR_DATA (0x200), proved IEEE-754 float32 Round-Half-to-Even requantization across stream125 (0x5b..0x5f), and recovered the 4x4 serpentine mesh SNAKE_16 = (0,1,2,3,7,6,5,4,8,9,10,11,15,14,13,12). Closed-form synthesis matched 81/81 compiled reference models bit-for-bit, 7 novel multi-tile kernels ran live with 3,368/3,368 exact bytes (max_abs_err = 0), and single-dispatch looped schedules (lt_1x* up to 1x2048x2048 and 1x4096x4096) peaked at 12.614 GMAC/s (64x576x1536) at 31.38 mW TPU rail power. 3. Zero-Vendor-Library Runtime (libopenbuenos): Reconstructing the two-layer Litebuf VII wire protocol (outer schema 0x0008 -> inner DIVE schema 0x00fe) over /dev/edgetpu-soc (0xED) and /dev/dma_heap/system produced tpu_baremetal_runner and libopenbuenos.a with DT_NEEDED = [libdl.so, libc.so] only, zero dlopen/dlsym imports, /proc/self/maps confirming zero Google TPU libraries loaded, and 2.3x-3.7x lower small-kernel wall latency than LiteRT (1.283-2.465 ms vs 4.021-5.641 ms). 4. Dynamic Param Swapping & Live LLM Inference: Because the RV64 DIVE interpreter writes to its in-image .bss tail during RegisterModel, libopenbuenos shares one bidirectional ext[1] DMA-BUF and decouples graph registration from weight tiles via dynamic param_slice IOVA swapping at Execute time. Unswapped execution peaked at 3,881 /dmabuf mappings; with param_slice swapping, llama.cpp (ggml-edgetpu) passed 223/223 test-backend-ops MUL_MAT checks (104/104 Q8_0 x F32) and generated coherent live text on SmolLM2-135M (44.1 mW TPU rail, 99.98%-99.99% cache hit rate, 0 evictions) and Qwen2.5-1.5B (1.8B Q8_0: 788 calls, 96,228 tiles, 30 registered handles, 0 evictions, 99.97% cache hit rate, 44.9 mW TPU rail). 5. Mode-A Fusion & Diagnosing LiteRT#12178: Patching chunk[5]'s 208 B 0x640 spline table in place yielded +0 B fused GEMM + SiLU/GELU (1.331 ms on 1x576x1536, 4/4 bit-exact probes; 12,288/12,288 exact spline bytes). Live silicon ablations on 0x980 w8[21]=1 (398 us, 16/16 exact) proved Tensor G5 lacks a native 4-bit DMA unpacker and instead implements a 1:2 UINT8-to-BF16 expander, explaining Google's DEQUANTIZE(INT4) -> FULLY_CONNECTED hang (LiteRT#12178) as a 2x MAC data starvation and unlocking native W8A16 (BF16) FP32-I/O GEMM (32/32 bit-exact FP32, max_f32_err = 0.0, 411-426 us).
Inferred
Four architectural takeaways explain why the open stack works end to end without vendor libraries: (1) Tensor G5 separates control orchestration (RV64GCV DIVE VM interpreting PutBits IOVA patches over Litebuf VII) from the 4x4 serpentine VLIW matrix/VICA engine, so once DGC0 container fields and the bit-77 shadow-CSR bank are modeled, any clean-room emitter can synthesize valid kernels without reproducing the 82.8 MB TFLite compiler; (2) binding weights into RegisterModel exhausts firmware graph handles and DMA-BUF VMAs across multi-layer transformers, whereas keying RegisterModel on shape and quantization metadata alone and swapping parameter slice IOVAs inside Execute keeps live firmware handles bounded to distinct layer shapes (30 handles for Qwen2.5-1.5B); (3) keeping 4-bit LLM weights packed at rest and unpacking once at load into cached uint8 DMA-BUFs matches Tensor G5's true 1:2 u8-to-BF16 DMA expander, sidestepping LiteRT#12178 and INT8 activation clipping; and (4) overwriting chunk[5]'s mandatory post-accumulation 0x640 MSB-flip LUT in place fuses SiLU/GELU at zero descriptor overhead.
Unknown & limitations
Clean-room userspace runtime, VLIW ISA specification, and single-subgraph DGC0 characterization only; FreeTPU bundles zero Google proprietary libraries, vendor kernel modules, firmware binaries, or device identifiers (users extract the 638,977 B RV64 DIVE interpreter from their own unlocked Pixel device at runtime). Multi-subgraph DMG0 containers, CONV_2D spatial stencil packing beyond tested unit shapes, Tensor G6 (santafe) bitstream deltas, and multi-core spill behavior beyond the 768 KiB per-core narrow SRAM ceiling remain outside verified closed-form emitter coverage. While SmolLM2-135M and Qwen2.5-1.5B produce coherent live output at 44.1-44.9 mW on the TPU rail, single-tile decode throughput in this baseline (0.14-0.89 t/s on SmolLM2-135M vs 173.88 t/s ARM64 CPU) remains host-dispatch bound pending chained multi-projection FFN fusion and W8A16 BF16 integration.
Architecture
FreeTPU replaces the entire proprietary Android userspace stack (Java/NNAPI, AIDL binder services, and vendor .so libraries) with four clean-room layers: (1) ggml-edgetpu inside llama.cpp (b12608), registering an EDGETPU ACCEL backend with 64-byte aligned host-accessible weight buffers and automatic ARM64 CPU fallback for RMS_NORM, ROPE, SOFT_MAX, MUL_MAT_ID, and GET_ROWS; (2) libopenbuenos, a dependency-free C11/C++17 static library implementing a power-of-two /dev/dma_heap/system slab pool (4 KiB..4 MiB, 36-bit IOVA mapped), a ref-counted shape-keyed LRU graph cache, a shared bidirectional ext[1] RV64 .bss mapping, a 16-credit async Litebuf VII ring, and a closed-form multi-tile VLIW GEMM emitter; (3) direct Linux UAPI transport over /dev/edgetpu-soc (buenos.ko ABI 1.4); and (4) Tensor G5 (buenos_a0) silicon comprising dual 1.12 GHz RV64GCV DIVE control cores, a 4x4 serpentine systolic PE mesh (16 tiles), 768 KiB/core narrow SRAM, 1 MiB unit memory, and 58 host IOMMU contexts.
Domain & data models
Executable containers follow a nested FlatBuffer hierarchy: DGC0 root -> DWS0 Package + DGD0 GraphData + ext[0] DiveVmIr (VLIW chunks chunk[0..6] and PutBits IOVA patch table) + ext[1] RV64GCV DIVE interpreter (638,977 B) + ext[2] parameters. For INT8 GEMM, ext[2] packs [n_groups, pad4(K)//4, n_blk, 4] weights encoded as (w + 128) & 0xff after optional int32[n_pad] biases. Quantization parameters live in a 125-bit stream (stream125 across CSRs 0x5b..0x5f) executing IEEE-754 float32 Round-Half-to-Even with post-round zp_out addition. Activation splines use a 208 B 0x640 LoadCoefficientTables payload carrying 47 float32 values (8 x 5 Horner coefficients + 7 breakpoints). For 0x980 w8, buenos_a0 encodes only 9 active bits: w8[21] (1:2 u8-to-BF16 enable) and w8[29:22] (unsigned 8-bit zero point), paired with CSR 0x56 = 0x0000026 and raw per-channel FP32 scales.
Workflows & state machines
The end-to-end recovery and runtime pipeline follows five stages: (1) Extract clean-room ABI and UAPI boundaries from a public Pixel factory image and AOSP edgetpu/gxp sources after an official bootloader unlock; (2) Run 150+ differential micro-TFLite probes and 1-bit/1-word live silicon ablations to recover 0xa40/0x9c0/0x940/0x980 DMA descriptors, the bit-77 shadow-CSR diffing model (HDR_DATA -> VICA_CSR -> CSR_040), and universal 2D DMA rewind; (3) Capture and verify byte-exact Litebuf VII command/response traffic to replace vendor libraries with libopenbuenos; (4) Scale to transformer decode by sharing the ext[1] RV64 .bss DMA-BUF, keying RegisterModel on shape/quantization while swapping weight ext[2] IOVAs dynamically per Execute, and emitting single-dispatch lt_1x* looped kernels up to 1x2048x2048 and 1x4096x4096; (5) Fold SiLU/GELU into chunk[5]'s 0x640 slot (+0 B) and unpack 4-bit weights once into uint8 DMA-BUFs to avoid LiteRT#12178.
API & protocol surfaces
Kernel transport drives /dev/edgetpu-soc (0xED) via EDGETPU_GET_CREDIT_SIZE (0x8008ed27 -> 16 credits), EDGETPU_CREATE_GROUP (0x4014ed06), EDGETPU_SET_EVENTFD (0x4008ed05), EDGETPU_ACQUIRE_WAKE_LOCK (0x0000ed1a), EDGETPU_MAP_DMABUF (0xc028ed11, 36-bit IOVA flag 0x8), EDGETPU_VII_LITEBUF_COMMAND (0xc038ed25: inline <= 96 B Execute 80 B / UnregisterModel 32 B; large > 96 B RegisterModel 144 B), and EDGETPU_VII_LITEBUF_RESPONSE (0xc018ed26), paired with /dev/dma_heap/system (DMA_HEAP_IOCTL_ALLOC 0xc0184800, DMA_BUF_IOCTL_SYNC 0x40086200). Outer mailbox schema 0x0008 wraps inner silicon.dive.api.runtime_commands schema 0x00fe with 32-bit sequence IDs. Userspace exposes openbuenos.h (ob_context_create, ob_buffer_alloc, ob_graph_register_dgc0, ob_graph_submit_async, ob_gemm_matmul_f32_q8_0, ob_gemm_extract_dive_interp_from_dgc0) and llama.cpp EDGETPU toggles (OPENBUENOS_MOCK, OPENBUENOS_PACKED_Q8, OPENBUENOS_VERBOSE, GGML_DISABLE_EDGETPU).

