Run this model with HF Jobs
Run metricspace/gemma4-E4B-it-litert-64k-mtp on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/metricspace/gemma4-E4B-it-litert-64k-mtp. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/metricspace/gemma4-E4B-it-litert-64k-mtp on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use metricspace/gemma4-E4B-it-litert-64k-mtp with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT-LM
How to use metricspace/gemma4-E4B-it-litert-64k-mtp with LiteRT-LM:
# LiteRT-LM runs on various platforms (Android, iOS, Windows, Linux, macOS, IoT, Web/WASM) # and supports many APIs (C++, Python, Kotlin, Swift, JavaScript, Flutter). # For platform-specific integration guides, please refer to the official developer website: # https://ai.google.dev/edge/litert-lm # To try LiteRT-LM, the easiest way is to use our CLI tool. # 1. Install the LiteRT-LM CLI tool: pip install -U litert-lm # 2. Download and run this model locally: # See: https://ai.google.dev/edge/litert-lm/cli # A single .litertlm file in the repo is picked automatically; otherwise the CLI asks which one to run # (or pass its name right after the repo id). litert-lm run \ --from-huggingface-repo=metricspace/gemma4-E4B-it-litert-64k-mtp \ --prompt="Write me a poem"
- Notebooks
- Google Colab
- Kaggle
metricspace/gemma4-E4B-it-litert-64k-mtp
Community-identical Gemma 4 E4B .litertlm bundle, binary-patched to support
a 64k context window on both CPU and GPU. Correct text output verified
against the community 32k bundle.
How it was built
Starting from litert-community/gemma-4-E4B-it-litert-lm (32k bundle, magic
cache dim 32003), tf_lite_prefill_decode and tf_lite_mtp_drafter are
binary-patched 32003 β 65003 (prime, avoids an XNNPack slow path that
triggers on composite dims like 65536 or 64006). The patch walks four layers
of the TFLite flatbuffer:
- Tensor shapes β every
shape[i] == 32003rewritten to 65003 (1 900 entries across decode / prefill / verify, 52 more in the drafter). - External buffers backing
arange(N)/arange(N)+512int32 index tables are regenerated for the new length, appended past the end of the file, andBuffer.offset/Buffer.sizefields rewritten. - Small RESHAPE / GATHER int32 shape-parameter buffers (β€ 256 B) that contain the baked dim are rewritten in place.
StableHLOCompositeOptions.CompositeAttributesFlexBuffer blobs of everyodml.cache_updatecomposite are walked and theirkv_cache_batch_size/key_size/value_size/scale_*attributes rewritten from 32003 β 65003. Missing step 4 gave graphs whose tensor shapes were right but whose per-layer composite attributes still referenced 32003 β the runtime then corrupts decode output to" agregado<pad><pad>...". All earlier-patchedrevisions had this bug; this release (v2) is the first with correct text output.
Everything else β weights, tokenizer, embedder, per-layer embedder, audio / vision towers, metadata β is byte-identical to the community source bundle.
Performance (Apple M5, LiteRT-LM built from main)
Median of 3 runs, 1024-token prefill / 256-token decode, synchronous
(--async=false), --conv_type=float, --max_num_tokens=2048 (typical
short-chat cache β see note below).
| Backend | Prefill tok/s | Decode tok/s | TTFT | vs CPU |
|---|---|---|---|---|
| CPU | ~30 | ~8.9 | ~40 s | 1Γ |
| GPU | ~512 | ~33 | ~2.0 s | 3.7Γ |
Decode cost scales roughly linearly with --max_num_tokens. For the long-
context floor: at --max_num_tokens=65000 (full bundle capacity), decode
lands near 21 tok/s with TTFT β 4.8 s for a 1024-token prompt.
Squeezing more out of the WebGPU delegate (optional, macOS)
The LiteRT-LM v0.10.1 prebuilt of libLiteRtTopKWebGpuSampler.dylib on
macOS exports only 3 of the 7 C symbols the runtime dynamically loads:
_UpdateConfig, _CanHandleInput, _HandlesInput, and
_SetInputTensorsAndInferenceFunc are compiled in but marked
non-external (was a private external) because of the
macos_exported_symbols.lds export list. The runtime then prints
GPU sampler unavailable. Falling back to CPU sampling. and every decode
step transfers the full logits tensor GPU β CPU for argmax.
You can promote those 4 symbols in the already-shipped dylib by rewriting
its LC_DYLD_EXPORTS_TRIE:
# ctox repo helper (scripts/models/patch_webgpu_sampler_exports.py) β
# appends a new 7-symbol exports trie at EOF, extends __LINKEDIT by 200 B,
# and ad-hoc re-signs the dylib. No source rebuild required.
python scripts/models/patch_webgpu_sampler_exports.py \
<LITERT_LM_PREBUILT_DIR>/libLiteRtTopKWebGpuSampler.dylib
On M5 / Gemma 4 E4B this patch makes the WebGPU sampler load cleanly but gave no measurable decode uplift in my runs β the decode bottleneck is WebGPU-delegate kernel-launch overhead, not the sampler. It's still worth applying for correctness (avoids one GPU β CPU transfer per step) and in anticipation of upstream sampler improvements.
Known upstream ceilings (not bundle-level)
- Speculative decoding (
--enable_speculative_decoding=true) crashes the WebGPU path withALREADY_EXISTS: wgpu::Instance already setwhen drafter and main model both try to create a WebGPU instance. On CPU it runs but produces 0 % acceptance rate β the drafter's first token never matches the verifier, so MTP is a strict slowdown on this runtime version. Keep it off until upstream ships a fix. --gpu_external_tensor_mode=truefails on Gemma 4 bundles withINVALID_ARGUMENT: bytes-per-token exceeds output tensor sizeβ the external-tensor path has a hard-coded token count that doesn't match Gemma 4's PLE layout. Can't enable.- Embedder / per-layer embedder are hardcoded to CPU in
runtime/components/embedding_lookup/embedding_lookup_text.ccon non-Android builds. Forcing them to GPU regresses throughput by β 9 Γ (E2B drops 68 β 9 tok/s) because the WebGPU delegate has no embedding-lookup kernel and silently falls back with bad scheduling. Leaving them CPU-pinned is correct. - No native Metal backend. The
gpu_artisanbackend referenced in--helpfullis internal-only. WebGPU β Dawn β Metal is the only GPU path on macOS today.
Run
litert-lm run --from-huggingface-repo metricspace/gemma4-E4B-it-litert-64k-mtp \
model.litertlm -b gpu --prompt "Explain transformers."
For the honest benchmark above use litert_lm_advanced_main:
litert_lm_advanced_main \
--model_path=model.litertlm \
--backend=gpu --async=false \
--max_num_tokens=2048 \
--conv_type=float \
--benchmark --benchmark_prefill_tokens=1024 --benchmark_decode_tokens=256
Choosing --max_num_tokens
This flag sets the magic-number rewrite target β the actual KV-cache size the runtime allocates and every attention step reads over. The bundle's dim 65003 is only a cap, not the working size. Decode cost is roughly linear in this value, so match it to your real use case:
- Short chats (β€ 2k tokens):
--max_num_tokens=2048β ~33 tok/s decode. - Medium documents (β€ 8k):
--max_num_tokens=8192β ~32 tok/s decode. - Long context (up to 64k):
--max_num_tokens=65000β ~21 tok/s decode, 4.8 s TTFT for a 1024-token prompt.
Build recipe
Patched with scripts/models/patch_tflite_cache_length.py (composite-
attribute walker included, v2) and repacked with
scripts/models/repack_litertlm.py from the ctox repository. The optional
sampler-dylib trie patcher lives in
scripts/models/patch_webgpu_sampler_exports.py.
- Downloads last month
- 255