Core AI model zoo

Gemma-4-E2B raw-Metal port — hand-written mixed-bit decode loop (ship doc)

What shipped: Gemma-4-E2B running on a fully hand-written Metal decode loop — no Core AI engine, no .aimodel, no MPSGraph. A 2.18 GB mmap’d pack of Google’s official QAT mixed-bit weights (int2/int4/int8 + PLE tables) is driven by 5 hand-tuned kernel files and a ~250-dispatch-per-token host sequence with on-GPU argmax. Lossless (token-exact vs the fp16 oracle, S1 gate 3/3 at every optimization step) and at LiteRT-LM speed parity on iPhone 17 Pro (same-afternoon interleaved A/B vs LiteRT-LM’s own benchmark entry point, 2026-07-15: raw median 53.7 vs LiteRT 50.5 tok/s; session best 56.2). Mac M4 Max: S=1 124.1 tok/s (engine int4lin path: 82.4).

Why this exists (the byte floor, and what sits above it)

Decode is bandwidth-bound. The shipped int4lin engine bundle reads ~2.0 GB/token; the QAT mixed-bit weights read 783 MB/token — but the stock engine graph cannot express the int2/int4/int8 mix + PLE gather at full efficiency (36.5 tok/s on iPhone = 28.6 GB/s effective). The raw loop exists to harvest the missing bandwidth with exact control over kernels and dispatch: 36.5 → 55–56 tok/s (43.8 GB/s effective vs LiteRT-LM’s 44.6 = 98%) with zero quality cost.

Architecture

Discipline (do not relax)

  1. mathMode .safe + literal op sequences. Fast math contracts/reassociates fp16 chains differently per kernel shape; near-ties then fork. This bit twice (see GEMMA4_METAL_LOOP_STATE.md traps). Any kernel or dispatch-order change requires the S1 token gate on device (G4CHAT_GATE=1 in CoreAIChat, oracle refs bundled) plus the python P0/P1 gates.
  2. Kernels version with the HOST, not the weights — they ship inside the app/kit bundle (Resources/g4msl, Gemma4Metal/g4msl), never next to the pack on HF.
  3. The gates are the only proof. tok/s claims come from settled fresh trial1 runs (see the A/B protocol in GEMMA4_METAL_LOOP_STATE.md SESSION 3); losslessness claims come from the S1 token gate, never from eyeballing text.

Known limits (v1)

Ship layout

HF mlboydaisuke/gemma-4-E2B-CoreAI, subtree raw-metal/gemma4_e2b_raw_metal/: gemma4_pack.bin (2.18 GB) + gemma4_pack.json + tokenizer/ (stock gemma tokenizer files) + metadata.json. Both platforms share the one pack; kit catalog id gemma-4-e2b-metal, CoreAIChat downloads the same subtree. Weights are Google’s Gemma-4-E2B QAT parameters from the official google/gemma-4-E2B-it-qat-mobile-transformers release — Gemma Terms of Use apply, and the model card credits the source. (Migrated from the earlier .litertlm extraction; bit-exact — see knowledge/gemma4-litertlm-to-official-migration.md.)