Ben Graville commited on
Commit
451139d
Β·
verified Β·
1 Parent(s): 36db1ae

docs: descriptive model card

Browse files
Files changed (1) hide show
  1. README.md +234 -41
README.md CHANGED
@@ -1,64 +1,257 @@
1
  ---
 
 
 
2
  library_name: coreai
3
- tags: [core-ai, aimodel, on-device, apple]
 
 
 
 
 
 
 
 
 
4
  ---
5
 
6
- # gemma-4-E4B-CoreAI
7
 
8
- Core AI `.aimodel` bundles converted for [Visible](https://visible.cx) on-device
9
- inference. **Derivative artifacts** β€” converted from the upstream checkpoints listed
10
- below; all original model licences apply and travel with these weights.
 
11
 
12
- | Bundle | Source checkpoint | Context | Functions | Producer | Created |
13
- |---|---|---|---|---|---|
14
- | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin` | google/gemma-4-E4B-it-qat-q4_0-unquantized | 4096 | main | coreai-core 1.0.0b2 | 20260817T105544Z |
15
- | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | google/gemma-4-E4B-it-qat-q4_0-unquantized | 4096 | main+prefill | coreai-core 1.0.0b2 | 20260817T111528Z |
 
 
 
 
 
16
 
17
  ## Provenance
18
 
19
- - Converted with the [coreai-model-zoo](https://github.com/john-rocky/coreai-model-zoo)
20
- conversion recipes on the pinned `apple/coreai-models` base
21
- `b1cb71b8522d99408059fa0b98b8742171bcb0b8` + the zoo python overlay.
22
- - Toolchain: `coreai-torch 0.4.1`, `coreai-core 1.0.0b2`, `coreai-opt 0.2.1`, `torch 2.9.0`.
23
- - Every bundle's inner `<name>.aimodel/metadata.json` reports
24
- `producer: "coreai-core 1.0.0b2"`.
25
- - Gemma bundles: `eos_token` is set to `<turn|>` (Gemma 4 ends a *turn* with `<turn|>`;
26
- stopping on the source's `<eos>` overruns the reply).
27
- - Bundles carry `runtime_env COREAI_CHUNK_THRESHOLD=1`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- Upstream sources: `google/gemma-4-E4B-it-qat-q4_0-unquantized`
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- ## High-context bundles β€” ctx 8192
 
 
 
 
 
33
 
34
- Same weights, same graph, same recipe as the 4096 bundles above β€” the exporter's
35
- `--max-ctx` only widens the dynamic sequence bound and the manifest's
36
- `language.max_context_length`. KV cache stays fp16.
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- | Bundle | Source checkpoint | Context | Functions | KV dtype | Producer | Created |
39
- |---|---|---|---|---|---|---|
40
- | `ctx8192/gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | google/gemma-4-E4B-it-qat-q4_0-unquantized | 8192 | main+prefill | fp16 | coreai-core 1.0.0b2 | 20260817T204459Z |
41
 
 
 
 
 
42
 
43
- ## High-context bundles β€” ctx 16384
44
 
45
- Same weights, same graph, same recipe as the 4096 bundles above β€” the exporter's
46
- `--max-ctx` only widens the dynamic sequence bound and the manifest's
47
- `language.max_context_length`. KV cache stays fp16.
 
 
 
 
 
 
 
48
 
49
- | Bundle | Source checkpoint | Context | Functions | KV dtype | Producer | Created |
50
- |---|---|---|---|---|---|---|
51
- | `ctx16384/gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | google/gemma-4-E4B-it-qat-q4_0-unquantized | 16384 | main+prefill | fp16 | coreai-core 1.0.0b2 | 20260817T204711Z |
 
52
 
 
 
 
 
53
 
54
- ## w4a8/ β€” int4 weights + INT8 activations (EXPERIMENTAL)
 
 
 
55
 
56
- > **EXPERIMENTAL β€” not qualified.** These bundles have not passed a Mac-side
57
- > oracle/parity gate or a device benchmark. Do not route production traffic to
58
- > them until they have.
 
59
 
60
- Same QAT int4 per-block-32 weights and same graph as gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64; the only difference is an int8 quantize/dequantize pair on the inputs of every F.linear, calibrated on 128 synthetic social-media-style samples (calibration_corpus.jsonl ships in the bundle). Intended as a prefill/TTFT lever. READ THE CAVEAT: coreai-opt documents the runtime fast path as W_INT8-A_INT8 and states a float weight path executes in floating point regardless of activation quantization. These weights use int4 scale-multiply dequant, not an int8 LUT, so the expected TTFT gain may be zero or negative. This bundle exists to be MEASURED. ctx 16384; includes chat_template.jinja, which the 4096 folders do not.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
- | Bundle | Source checkpoint | Context | Functions | Bytes | Producer | Created |
63
- |---|---|---|---|---|---|---|
64
- | `w4a8/gemma4_e4b_qat_decode_int4lin_a8_tbl_pf64` | google/gemma-4-E4B-it-qat-q4_0-unquantized | 16384 | main+prefill | 3,990,690,638 | coreai-core 1.0.0b2 | 20260817T223600Z |
 
1
  ---
2
+ license: gemma
3
+ license_link: https://ai.google.dev/gemma/terms
4
+ base_model: google/gemma-4-E4B-it-qat-q4_0-unquantized
5
  library_name: coreai
6
+ pipeline_tag: text-generation
7
+ tags:
8
+ - core-ai
9
+ - aimodel
10
+ - apple-silicon
11
+ - on-device
12
+ - quantized
13
+ - int4
14
+ - qat
15
+ - gemma4
16
  ---
17
 
18
+ # Gemma 4 E4B β€” Core AI (.aimodel)
19
 
20
+ `google/gemma-4-E4B-it-qat-q4_0-unquantized` converted to Core AI `.aimodel` bundles for
21
+ Apple silicon by the [Visible](https://visible.cx) project. **Derivative artifacts**:
22
+ Google's QAT-trained weights rounded onto the int4 grid they were trained for and
23
+ re-expressed as a Core AI graph.
24
 
25
+ Gemma 4 E4B uses **Per-Layer Embeddings**, so the working bundles here take a large
26
+ embedding gather table as a *static graph input* rather than carrying it in the graph. That
27
+ table ships in this repo (`ios-frontend/`) and **the bundles do not run without it** β€” see
28
+ Contents and Requirements, because a missing table produces a bare input-arity error that
29
+ mentions nothing about tables.
30
+
31
+ This model does grammar-constrained structured output correctly (10/10 enum-clean on
32
+ Visible's schema) and it is **slow and memory-bound on a 16 GB Mac**. Both facts are
33
+ measured below.
34
 
35
  ## Provenance
36
 
37
+ | | |
38
+ |---|---|
39
+ | Base checkpoint | `google/gemma-4-E4B-it-qat-q4_0-unquantized` (ungated) |
40
+ | Recipe (decode) | `export_gemma4_decode_pipelined.py int4lin` β€” zoo recipe `gemma-4-e4b`, `status = "verified"` |
41
+ | Recipe (pf64) | `export_gemma4_pf_pipelined.py --pf 64` with `--tbl` and `--raw-dir` pointed at the gather table below |
42
+ | Toolchain base | `apple/coreai-models` @ `b1cb71b8522d99408059fa0b98b8742171bcb0b8` + the [coreai-model-zoo](https://github.com/john-rocky/coreai-model-zoo) python overlay |
43
+ | Toolchain | `coreai-torch 0.4.1`, `coreai-core 1.0.0b2`, `coreai-opt 0.2.1`, `torch 2.9.0` |
44
+ | Producer fingerprint | `coreai-core 1.0.0b2` on every inner asset `metadata.json` |
45
+ | Weight format | **int4, per-block-32** (`int4lin`, symmetric-with-clipping) β€” the ggml q4_0 grid the QAT checkpoint was trained on |
46
+ | Vocab | 262,144 |
47
+ | Conversion | 241 s / 44.0 GB peak RSS (decode); 196 s / 42.7 GB (pf64); Linux x86_64 |
48
+
49
+ "QAT-unquantized" means QAT-*trained*, stored full width; the int4 rounding happens at
50
+ export onto the grid training already targeted.
51
+
52
+ `_tbl` = the PLE gather table is bound as a static graph input. `_pf64` = a second
53
+ entrypoint, `prefill`, with a static query width of 64
54
+ (`function_map: {"main": ["main", "prefill"]}`).
55
+
56
+ ## Contents
57
+
58
+ ### Bundles
59
+
60
+ | Path | Bytes | Context | Functions | Status |
61
+ |---|---:|---|---|---|
62
+ | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | 3,989,986,858 | 4096 | main + prefill | the working bundle |
63
+ | `ctx8192/gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | 3,989,986,830 | 8192 | main + prefill | same weights, wider manifest |
64
+ | `ctx16384/gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` | 3,989,986,844 | 16384 | main + prefill | same weights, wider manifest |
65
+ | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin` | 3,989,205,604 | 4096 | main | **BLOCKED β€” see below** |
66
+
67
+ Each folder holds `<name>.aimodel/` (`main.mlirb` β‰ˆ 3.96 GB, `main.hash`, asset
68
+ `metadata.json`), a bundle-level `metadata.json`, and `tokenizer/` (`tokenizer.json`
69
+ 32,169,626 B, `tokenizer_config.json`, `generation_config.json`, `chat_template.jinja`
70
+ 18,569 B).
71
+
72
+ **The three `_tbl_pf64` folders are the same weights.** `--max-ctx` does not change the
73
+ exported graph β€” identical function signatures, identical state descriptors, identical
74
+ export peak RSS at every value. It changes exactly one thing:
75
+ `language.max_context_length` in the bundle manifest. This was proved with a same-config
76
+ control re-export whose `main.mlirb` differed from its twin by *more* bytes than a
77
+ 4096β†’8192 pair does. Pick the folder whose manifest integer matches the window you intend
78
+ to run; a future context bump is a manifest edit, not a conversion.
79
+
80
+ ### The PLE gather-table sidecar β€” required, not optional
81
+
82
+ | Path | Files | Bytes |
83
+ |---|---|---:|
84
+ | `ios-frontend/gemma4_e4b_qat_gather_raw/` | 7 | 3,601,859,815 |
85
+
86
+ | File | Bytes |
87
+ |---|---:|
88
+ | `embed_per_layer.i8` | 2,818,572,288 |
89
+ | `embed_tokens.i8` | 671,088,640 |
90
+ | `proj.f32` | 110,100,480 |
91
+ | `embed_per_layer.scale.f32` | 1,048,576 |
92
+ | `embed_tokens.scale.f32` | 1,048,576 |
93
+ | `proj_norm.f32` | 1,024 |
94
+ | `meta.json` | 231 |
95
+
96
+ Every `_tbl` bundle binds `ple_table` (from `embed_per_layer.i8`) and `ple_scale` (from
97
+ `embed_per_layer.scale.f32`) as **static** graph inputs. Without them the bundle fails to
98
+ load with a bare input-arity error naming `ple_table`/`ple_scale` and saying nothing about
99
+ tables β€” a diagnosis this project got wrong once already. The sidecar is published here so
100
+ the repo is self-contained for both loading and a pf64 rebuild.
101
 
102
+ ## Requirements
103
 
104
+ - **Apple silicon Mac**, Core AI runtime.
105
+ - **Engine contract: 4 inputs** β€” `input_ids`, `position_ids`, plus the static `ple_table`
106
+ and `ple_scale`. Two engines can take that:
107
+ - **Pipelined engine** β€” binds the static inputs zero-copy over the caller's buffer, but
108
+ **does not support logits**, so no grammar-constrained decoding.
109
+ - **Sequential engine** β€” the only logits-capable engine, and therefore the only one
110
+ guided decoding can run on. It requires the static-input binding that accepts `>= 2`
111
+ inputs and binds everything beyond `input_ids`/`position_ids` from
112
+ `EngineOptions.staticInputBuffers`; an extra input with no buffer is rejected by name.
113
+ It pays a **one-time copy of every constant input at load β€” 2.69 GB for E4B** β€” because
114
+ its submission path materialises a foreign buffer-backed view on every pass otherwise
115
+ (measured: binding the table as a raw view drove peak footprint to 14.9 GB and
116
+ segfaulted the runtime mid-prefill).
117
+ - **States:** `keyCache` / `valueCache` `Float16, 24 Γ— 1 Γ— 2 Γ— ? Γ— 512`. Dynamic sequence
118
+ dim β†’ `GrowingKVCache` (initial 256, doubling).
119
+ - **KV cost: 98,304 bytes per token** (fp16) β€” 403 MB at 4096, 805 MB at 8192, 1.61 GB at
120
+ 16384.
121
+ - **Memory envelope, 16 GB Mac** (Metal `recommendedMaxWorkingSetSize` β‰ˆ 10.7 GB):
122
 
123
+ | | |
124
+ |---|---|
125
+ | Weights (`main.mlirb`) | 3.96 GB |
126
+ | PLE gather table (static input) | 2.82 GB |
127
+ | Base | **6.78 GB** |
128
+ | Affordable context, fp16 KV | **~39,900 tokens** |
129
 
130
+ So KV was never the ceiling at this tier β€” 16384 costs 1.61 GB against 3.92 GB of
131
+ headroom. **The real constraint is the PLE table**, which dwarfs the KV cache at any
132
+ context worth shipping. Measured peak footprint on a guided run was **10.7 GB**, i.e.
133
+ right at the working-set edge.
134
+ - **Run one E4B campaign per process.** Wired memory from a Gemma campaign is not reclaimed
135
+ until the process exits; a second full E4B run in the same session drove wired memory to
136
+ 13.2 GB and left the machine with 63 MB free.
137
+ - Sliding-window note: E4B interleaves 20 sliding and 4 full attention layers across 42
138
+ layers collapsed to 24 KV slots. The export models the window in the *mask*, not in
139
+ memory β€” sliding layers ride full-length slots and zero-pad head_dim 256β†’512, so 83.3% of
140
+ KV bytes sit in windowed slots and 41.7% is dead padding. A ring-buffered sliding cache
141
+ would be ~6Γ— cheaper per token. That is model authoring, not a flag, and it is not needed
142
+ at this tier.
143
+ - `runtime_env COREAI_CHUNK_THRESHOLD=1` travels with the recipe. (Both engines now derive
144
+ the static query width from the graph, so the older host-side rules about warmup length
145
+ and chunk threshold are retired.)
146
 
147
+ ## Measured performance
 
 
148
 
149
+ Measured on a **16 GB M2 Pro Mac (macOS 27 beta)** with the out-of-process `coreai-repro`
150
+ harness: 10 real enrichment rows (5 POST, 5 COMMENT), the app's live `app_litert` JSON
151
+ schema, greedy decoding through `respondJSON(to:schema:)`, `reset()` per row, **sequential
152
+ engine with the PLE tables bound as static inputs**, 128-token cap.
153
 
154
+ `gemma4_e4b_qat_decode_int4lin_tbl_pf64`:
155
 
156
+ | | |
157
+ |---|---|
158
+ | Load | 26.1 s |
159
+ | Guided JSON parse | **10/10** |
160
+ | Enum-clean | **10/10** |
161
+ | s/row | **21.89** (POST) / **12.36** (COMMENT) |
162
+ | Decode | 6.7–15.9 tok/s, mean **10.2** |
163
+ | TTFT | 2.8–9.7 s |
164
+ | Peak footprint | 10.7 GB |
165
+ | Stop token | `<turn\|>`, clean self-stop on every row |
166
 
167
+ **The grammar is close to free; prefill is the cost.** Decode throughput is essentially
168
+ unchanged from unguided pipelined measurements (9.3–11.0 β†’ 10.2 tok/s). Guided rows cost
169
+ ~25% more than unguided ones because the sequential engine's prefill is step-synchronous,
170
+ not because of the constraint.
171
 
172
+ **Enum conformance is entirely the grammar's doing, and it is decisive.** Unguided, this
173
+ model invented an off-schema `motivation` value in **all ten** rows (`empowerment`,
174
+ `advocacy`, `informative`…, none of them schema members). Guided, all ten are correct β€” not
175
+ because the model improved, but because an off-enum token is unsamplable.
176
 
177
+ **Context for the cost.** On the same harness and machine, LFM2.5-1.2B runs 1.77/1.30 s/row
178
+ and Gemma 4 E2B runs 7.06/4.27. This bundle is roughly **12Γ— the 1.2B's cost per row** and
179
+ is memory-bound at 10.7 GB peak. Published Mac figures of ~55.8 tok/s for E4B decode should
180
+ be read as bigger-Mac figures.
181
 
182
+ Nothing was measured above 4096 tokens. The `ctx8192`/`ctx16384` folders carry the 4096
183
+ graph, so throughput should be unchanged, but quality at depth is a real open question:
184
+ beyond ~4k the long-range signal rides on 4 full-attention layers, and widening the manifest
185
+ does not widen the sliding layers' 512-token window.
186
 
187
+ ## Qualification status
188
+
189
+ | Artifact | Status |
190
+ |---|---|
191
+ | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin_tbl_pf64` (ctx 4096) | **QUALIFIED β€” measured, viable, not a default.** 10/10 parse, 10/10 enum-clean on the harness above. Slow and memory-bound at the 16 GB tier; it earns a slot only where Gemma's output specifically is wanted. |
192
+ | `ctx8192/…_tbl_pf64` | **QUALIFIED BY EQUIVALENCE, UNMEASURED AT DEPTH** β€” same weights, same graph, wider manifest integer. No run above 4096 tokens. |
193
+ | `ctx16384/…_tbl_pf64` | **QUALIFIED BY EQUIVALENCE, UNMEASURED AT DEPTH** β€” as above. |
194
+ | `gpu-pipelined/gemma4_e4b_qat_decode_int4lin` | **BLOCKED β€” does not load on any current code path.** |
195
+
196
+ ### Why the non-`tbl` decode bundle is blocked
197
+
198
+ It declares a **different third input** from the `_tbl` family:
199
+
200
+ ```
201
+ LOAD FAILED: invalidInputType("Expected 2 inputs, got 3:
202
+ [\"input_ids\", \"position_ids\", \"ple_tokens\"]")
203
+ ```
204
+
205
+ `ple_tokens`, not `ple_table`/`ple_scale`. The Gemma architecture description in the kit
206
+ knows only the pair (`ple_table` β†’ `embed_per_layer.i8`, `ple_scale` β†’
207
+ `embed_per_layer.scale.f32`), so **no code path binds `ple_tokens`** and the table sidecar
208
+ in this repo does not help. It is unusable as exported. The fix is a re-export with `--tbl`
209
+ so E4B decode and prefill are the same graph family, not an extension to the kit; until
210
+ then, use the `_tbl_pf64` bundle, which is a strict superset (it carries both `main` and
211
+ `prefill`).
212
+
213
+ It is kept published rather than deleted because it is a fingerprinted, immutable artifact
214
+ that earlier revisions pin, and because the failure it demonstrates is worth documenting.
215
+
216
+ ## Verification
217
+
218
+ - **Producer fingerprint:** every inner `<name>.aimodel/metadata.json` reads
219
+ `producer: "coreai-core 1.0.0b2"`. `coreai-torch 0.4.0` / `coreai-core 1.0.0b1` produce
220
+ ODIE-poisoned bundles that abort **in-process** at load, taking the host application down
221
+ uncatchably. The field is in the **inner asset** metadata; the bundle manifest never
222
+ carries a `producer` field, so checking the manifest condemns every good bundle.
223
+ - **Stop token, verified per folder:** all four declare `eos_token = "<turn|>"`. Gemma 4
224
+ ends a *turn* with `<turn|>` (id 106); the raw upstream `<eos>` ends the whole sequence,
225
+ and a host stopping on it overruns every reply. The exporter copies the upstream tokenizer
226
+ config verbatim and does **not** apply this correction β€” it is a post-export patch, and it
227
+ was checked on these bundles rather than assumed. `generation_config.json` independently
228
+ lists `eos_token_id: [1, 106, 50]`.
229
+ - **`pf64` is worth 7.7Γ— on time-to-first-token, measured.** The A/B was run on the E2B
230
+ sibling (same prompt, same weights, differing only in whether the `prefill` function was
231
+ exported): TTFT 35.74 s β†’ 4.65 s, s/row 45.04 β†’ 8.83. The non-`pf64` variant also failed
232
+ partway through a ten-row campaign with the runtime's footprint ballooning past 37 GB
233
+ across ~2,800 single-token passes β€” it is not merely slower, it is not durable. Prefer
234
+ `_pf64` everywhere; that is why no plain `_tbl` E4B bundle exists here.
235
+ - **Determinism:** `.aimodel` conversion is not byte-reproducible; the same command twice on
236
+ one box gives different `main.mlirb` bytes. Integrity rests on per-file SHA-256 of the
237
+ exact published artifact, never on rebuilding.
238
+ - **No oracle gate has been run on this model.** The 10/10 results above are behavioural
239
+ (parse rate, enum conformance, clean stop) on Visible's own task. A decode oracle against
240
+ an fp32 reference β€” the gate that would catch an int4 or kernel-level numerics error β€” has
241
+ not been run on any Gemma 4 bundle in this catalog.
242
+
243
+ ## License
244
+
245
+ The upstream QAT checkpoint carries Apache-2.0 metadata and is ungated, but it is a Gemma
246
+ model and **the Gemma terms apply downstream**, which is why this repo declares
247
+ `license: gemma`. Use is subject to the
248
+ [Gemma Terms of Use](https://ai.google.dev/gemma/terms), the
249
+ [Gemma Prohibited Use Policy](https://ai.google.dev/gemma/prohibited_use_policy) and the
250
+ [Gemma 4 license](https://ai.google.dev/gemma/docs/gemma_4_license). Those obligations
251
+ travel with any redistribution of these bundles, including the gather-table sidecar, which
252
+ is derived from the same weights. Visible's contribution is the conversion, not the weights.
253
+
254
+ ---
255
 
256
+ *Additional variants may land in this repo after this card was written; the tables above
257
+ describe its contents as listed at the time of writing.*