mlboydaisuke commited on
Commit
8afedeb
Β·
verified Β·
1 Parent(s): 767d34e

E4B card: official-QAT int4 pipelined bundles

Browse files
Files changed (1) hide show
  1. README.md +91 -0
README.md ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: gemma
3
+ base_model: google/gemma-4-E4B-it-qat-q4_0-unquantized
4
+ tags:
5
+ - coreai
6
+ - aimodel
7
+ - apple-silicon
8
+ - on-device
9
+ - gemma-4
10
+ - qat
11
+ - gpu-pipelined
12
+ pipeline_tag: text-generation
13
+ ---
14
+
15
+ # Gemma 4 E4B (text) β€” Apple Core AI (`.aimodel`)
16
+
17
+ **Gemma 4 E4B's text decoder converted to Apple's Core AI** (the Core ML successor announced
18
+ at WWDC26), running on iOS 27 / macOS 27 via Apple's `coreai-pipelined` GPU engine β€” **zero
19
+ custom kernels, greedy oracle 8/8 exact vs the fp32 Hugging Face reference on the Mac GPU and
20
+ the iPhone GPU (iPhone is 24/24 token-identical to the Mac on the determinism probe)**.
21
+
22
+ Converted **directly from Google's official QAT release**
23
+ [google/gemma-4-E4B-it-qat-q4_0-unquantized](https://huggingface.co/google/gemma-4-E4B-it-qat-q4_0-unquantized):
24
+ bf16 weights **trained for q4_0 rounding**, and q4_0 *is* this bundle's quantization class
25
+ (per-block-32 absmax linear int4) β€” Google publishes these checkpoints as "preserving similar
26
+ quality to bfloat16", so this int4 conversion carries that guarantee **by design**, not by
27
+ post-hoc gating.
28
+
29
+ > Requires the iOS 27 / macOS 27 beta. Conversion code, knowledge base, engine patch stack:
30
+ > **[coreai-model-zoo](https://github.com/john-rocky/coreai-model-zoo)** β€”
31
+ > model card: [`zoo/gemma4-e4b.md`](https://github.com/john-rocky/coreai-model-zoo/blob/main/zoo/gemma4-e4b.md).
32
+
33
+ ## Measured (greedy; M4 Max / iPhone 17 Pro, settled device)
34
+
35
+ | config | files | size | M4 Max decode / prefill | iPhone decode / prefill |
36
+ |---|---|---|---|---|
37
+ | β˜… **provider** (runs BOTH platforms) | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin/` + `ios-frontend/gemma4_e4b_qat_gather_raw/` | 3.7 + 3.4 GB | 53.2 / 62.6 | **15.1 / 21.3** |
38
+ | β˜… **provider, iPhone-ready AOT** | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin_aotc_h18p/` (precompiled `.aimodelc`, **h18p = iPhone 17 Pro class only**) + the same tables | 3.7 + 3.4 GB | β€” | same as above β€” skip the AOT step |
39
+ | **tbl** (Mac-fastest) | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl/` + the two `embed_per_layer.*` table files | 3.7 + 2.7 GB | **55.8** / 61.0 | not viable (3.7 GB graph + 2.7 GB owned tables > the ~6.4 GB entitled limit) |
40
+
41
+ On iPhone the working set stays tiny β€” measured peak footprint **2.2 GB** (4.2 GB headroom):
42
+ the PLE table rides as a clean mmap and the AOT executable pages are evictable. Both phases
43
+ land exactly on the bandwidth model (~2.1 GB int4/token).
44
+
45
+ ## What E4B is (config + checkpoint verified)
46
+
47
+ Clean **dense** model β€” no MoE. 42 layers (full attention every 6th), hidden 2560,
48
+ intermediate 10240 uniform, 8 query heads / **2 KV heads**, dual head_dim 256/512, 18
49
+ KV-shared layers (the engine bundle stacks the 24 non-shared layers into ONE unified padded
50
+ KV pair), per-layer embeddings (the [262144, 10752] int8 table ships in
51
+ `ios-frontend/gemma4_e4b_qat_gather_raw/`), final-logit softcap 30. The QAT checkpoint prunes
52
+ the never-used KV projections on the shared layers β€” the zoo's loader handles both layouts.
53
+
54
+ ## Run contract (each item is load-bearing)
55
+
56
+ Full story + traps:
57
+ [pipelined-engine page](https://github.com/john-rocky/coreai-model-zoo/blob/main/knowledge/pipelined-engine.md).
58
+
59
+ 1. Swift stack = `apple/coreai-models` + the zoo's patch stack
60
+ ([`apps/*.patch`](https://github.com/john-rocky/coreai-model-zoo/tree/main/apps), in
61
+ order). The β˜… provider bundle needs `EngineOptions.perTokenInputProvider`
62
+ (`coreai-pipelined-per-token-inputs.patch`); the tbl bundle needs
63
+ `EngineOptions.staticInputBuffers` (`coreai-pipelined-static-inputs.patch`).
64
+ 2. Provider mode: per token, fill `ple_tokens [1,1,42,256]` fp16 from the table dump β€”
65
+ `row = i8[id] * scale[id] * sqrt(256)`, mmap-gathered (~0.1 ms). tbl mode: bind
66
+ `ple_table` ← `embed_per_layer.i8` and `ple_scale` ← `embed_per_layer.scale.f32` as
67
+ **OWNED `storageModeShared` MTLBuffers** (buffer-backing traps in the knowledge page).
68
+ 3. `COREAI_CHUNK_THRESHOLD=1` **before** engine creation; **never call `engine.warmup()`**
69
+ (S=1 graph; a 1-token generate after load is the warmup).
70
+ 4. iPhone: **AOT is mandatory** (the 3.7 GB-constants graph crashes the on-device
71
+ specializer) β€” use the precompiled `_aotc_h18p/` bundle, or
72
+ `xcrun coreai-build compile <bundle>.aimodel --platform iOS --preferred-compute gpu
73
+ --architecture h18p --expect-frequent-reshapes` and point `metadata.json`'s
74
+ `assets.main` at the `.aimodelc`. Ship the
75
+ `com.apple.developer.kernel.increased-memory-limit` entitlement as headroom insurance,
76
+ and bench a **settled** device (a just-unlocked iPhone under-reads ~35%).
77
+
78
+ Reproduce from scratch (oracle + tables are checkpoint-derived β€” regenerate for any new
79
+ weights): [`conversion/export_gemma4_decode_pipelined.py`](https://github.com/john-rocky/coreai-model-zoo/blob/main/conversion/export_gemma4_decode_pipelined.py)
80
+ with `--hf-id google/gemma-4-E4B-it-qat-q4_0-unquantized`.
81
+
82
+ ## License
83
+
84
+ Gemma is provided under and subject to the **Gemma Terms of Use**
85
+ (https://ai.google.dev/gemma/terms). These `.aimodel` bundles are Model Derivatives of
86
+ [google/gemma-4-E4B-it-qat-q4_0-unquantized](https://huggingface.co/google/gemma-4-E4B-it-qat-q4_0-unquantized);
87
+ by downloading or using them you agree to those terms, including the
88
+ [Gemma Prohibited Use Policy](https://ai.google.dev/gemma/prohibited_use_policy).
89
+
90
+ Sibling repo (E2B, incl. its own official-QAT bundles):
91
+ [gemma-4-E2B-CoreAI](https://huggingface.co/mlboydaisuke/gemma-4-E2B-CoreAI).