Gemma 4 E2B — INT4 ExecuTorch .pte for Raspberry Pi 5
INT4-quantized, ExecuTorch-lowered .pte of google/gemma-4-e2b-it, packaged for Raspberry Pi 5 (Cortex-A76, 8 GB) deployment via the ExecuTorch 1.2.0 Python runtime with the XNNPACK backend.
This artifact is the deployable output of the full export → quantize → lower → runtime pipeline documented at:
Pi 5 decode rate varies ±15% across sessions (small per-prompt sample, thermal state, default schedutil cpufreq governor). For stable benchmarking, pin the governor to performance and let the SoC return to <55°C between runs.
Output quality: bit-exact 9/9 token match against the FP32 reference on the canonical prompt.
The Pi 5 decode is approximately 7.7× slower than llama.cpp on identical hardware. The shipped build uses XnnpackPartitioner(per_op_mode=True) to work around an ARM XNNPACK rejection bug in ExecuTorch 1.2.0 — initially believed to be the entire cause of the gap. Three controlled follow-up experiments (PT2E quantization, config_precisions=DYNAMIC_QUANT, ExecuTorch nightly 1.4.0.dev with the default fused partitioner) confirm that the partitioner mode is not the bottleneck — even with 508 fused subgraphs on ARM nightly the decode rate does not improve. The ARM XNNPACK rejection itself IS fixed in nightly; stable 1.3+ should ship the fix. The remaining gap likely lives in the XNNPACK kernel format (vs llama.cpp's GGUF Q4_K_M hand-tuned ARM kernels), the KleidiAI link status of the executorch wheel's XNNPACK build, or the external KV-cache materialization pattern. Full diagnosis with the measured three-way Pi benchmark at KNOWN_ISSUES.md #1 in the source repo. If maximum Pi 5 decode throughput is the priority, llama.cpp is the appropriate tool today.
Quick use on a Raspberry Pi 5
bash
1# 1. Download the bundle (~5.2 GB)2pip install --user huggingface_hub
3hf download bamb00boy/gemma4-e2b-int4-executorch-pi5 --local-dir ~/gemma4
45# 2. Set up the runtime environment6cd ~/gemma4
7python3 -m venv .venv &&source .venv/bin/activate
8pip install --upgrade pip
9pip installtorch==2.11.0 executorch==1.2.0 transformers==5.5.3
1011# 3. Verify (should print "RESULT: PASS" and "The capital of France is **Paris**.")12python pi_runner.py --verify
1314# 4a. One-shot generation15python pi_runner.py "Your prompt here" --max-new-tokens 501617# 4b. Or an interactive multi-turn chat (KV-cache reused across turns)18python gemma4_terminal_chat.py
19# Type a message + Enter. /help for commands. Ctrl+C or Ctrl+D to exit.
The Pi setup guide (OS install, performance tuning, SSH) lives in docs/pi5_setup.md in the source repo.
Use on other hosts
The .pte runs on any host with ExecuTorch 1.2.0 + XNNPACK. It has been validated on:
aarch64 Linux (Raspberry Pi 5, Ubuntu Server 24.04)
macOS arm64 (Apple Silicon, used as the development reference)
x86_64 Linux is expected to work (XNNPACK supports it) but is untested by this project.
What's quantized, what's not
Component
Treatment
nn.Linear weights (~3.1 B params)
INT4 weight-only via torchao's Int8DynamicActivationIntxWeightConfig (stored unpacked as INT8 bytes on disk)
embed_tokens_per_layer (~2.35 B params, the "E2B" trick)
INT8 per-row via a custom Int8Embedding module (see source repo's scripts/_int8_embedding.py)
embed_tokens (~0.4 B params)
FP32 — Gemma 4's model code performs direct weight slicing, which is incompatible with quantized tensor wrappers
Layer norms, RoPE buffers, biases
FP32
Runtime K/V cache
FP32, externalized as program inputs/outputs (see source repo's scripts/_external_cache.py)
Disk size: 5.14 GB. Runtime cache footprint: 18.9 MB across 15 layers (12 sliding-window @ head_dim=256, 3 full-attention @ head_dim=512).
Architecture & shape constraints
Sequence length: padded to 511 tokens at runtime (the .pte shape-specializes to the upper bound of the dynamic dim).
Decode: token-by-token (no batched prefill in this build).
Maximum total context: 511 tokens (prompt + generated combined).
Cache: externalized — 90 cache tensors are passed as graph inputs and 45 are returned as graph outputs each call (one K + one V per layer × 15 layers + sentinel for prefill vs decode).
License
The weights in this file are derived from google/gemma-4-e2b-it and are licensed under Apache License 2.0 by Google DeepMind. Use is subject to:
This is a derivative work: INT4 weight-only quantization of nn.Linear weights and INT8 per-row quantization of embed_tokens_per_layer, followed by ExecuTorch program lowering with the XNNPACK backend. No additional fine-tuning has been performed.
The packaging code (pi_runner.py, gemma4_terminal_chat.py, and the export/quantize/lower pipeline) is released under MIT — see the source GitHub repo.
If this artifact saved you time or you'd like to help fund continued work on on-device LLM packaging, you can support the project at ko-fi.com/bamb00boy. All contributions are appreciated and go directly toward more experiments, benchmarks, and open releases.