mlboydaisuke commited on
Commit
8b464e4
·
verified ·
1 Parent(s): 0807d1b

Card, coreai-kit.json manifest, source config.json

Browse files
Files changed (3) hide show
  1. README.md +186 -0
  2. config.json +45 -0
  3. coreai-kit.json +66 -0
README.md ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: coreai
3
+ license: apache-2.0
4
+ base_model: ibm-granite/granite-embedding-97m-multilingual-r2
5
+ tags:
6
+ - coreai
7
+ - sentence-similarity
8
+ - feature-extraction
9
+ - apple-silicon
10
+ - on-device
11
+ - modernbert
12
+ language:
13
+ - multilingual
14
+ - ja
15
+ - en
16
+ pipeline_tag: sentence-similarity
17
+ ---
18
+
19
+ Core AI is Apple's on-device ML runtime in iOS 27 / macOS 27 and the successor to Core ML: PyTorch models are exported with Apple's `coreai-torch` (LLMs: `coreai.llm.export`) into `.aimodel` bundles that run on the GPU or the Neural Engine, e.g. Qwen3-8B 4-bit decodes at 94 tok/s on an M4 Max GPU, MLX 90 under the same protocol ([apple-silicon-llm-bench](https://github.com/john-rocky/apple-silicon-llm-bench), macOS 27 beta, 2026-06).
20
+
21
+ <!-- gen-cards:devicemark begin (managed by scripts/gen-cards + tools/devicemark_row.py — edit cards.json, not this block) -->
22
+ This model has no row on [DeviceMark](https://devicemark.github.io/), the on-device LLM leaderboard.
23
+ <!-- gen-cards:devicemark end -->
24
+
25
+ # Granite-Embedding-97M-Multilingual-R2 — Core AI export
26
+
27
+ Zoo card, recipe and gate transcript: [coreai-model-zoo/models/granite-embedding-97m](https://github.com/john-rocky/coreai-model-zoo/blob/main/models/granite-embedding-97m/README.md).
28
+
29
+ IBM's 97M-parameter **multilingual text embedder** — a ModernBERT encoder, 384-d CLS-pooled
30
+ unit vectors, Japanese and English among its languages — as a static `.aimodel` for macOS 27
31
+ and, ahead-of-time compiled, for the iPhone 17 Pro.
32
+ [`ibm-granite/granite-embedding-97m-multilingual-r2`](https://huggingface.co/ibm-granite/granite-embedding-97m-multilingual-r2)
33
+ (Apache-2.0, revision `835ad1408…`) is the **smallest embedder in this catalog** (390 MB fp32,
34
+ against 1.2 GB for EmbeddingGemma-300m and 1.1 GB for Qwen3-Embedding-0.6B) and its **first
35
+ encoder-architecture one** — every other embedder here is a causal decoder run as an encoder.
36
+ Its retrieval quality relative to those three was **not** measured here: the fixture set below
37
+ is a parity instrument (35 texts, 4 queries, 12 documents), not a benchmark.
38
+
39
+ **This is an encoder, not a generator** — one forward over the right-padded grid returns one
40
+ unit vector. No autoregressive loop, no KV cache, no LM head. It runs as a plain `.aimodel`
41
+ through raw `AIModel.run` (like the vision encoders), not the pipelined generate engine.
42
+
43
+ Architecture (`model_type: modernbert`): 12 layers, hidden 384, 12 heads × 32, GLU MLP 1536
44
+ (SiLU), vocabulary 180,000, biasless everything (attention, MLP, LayerNorm ε 1e-5). Global
45
+ attention at layers **0, 3, 6, 9** (RoPE θ 150,000); the other eight are **local**, a sliding
46
+ window of inclusive radius 64 (129 keys per interior query, RoPE θ 160,000). Layer 0 has no
47
+ attention pre-norm (the embedding LayerNorm serves). Pooling is CLS → L2 normalize, both in the
48
+ graph.
49
+
50
+ ## Graph contract
51
+
52
+ ```
53
+ input "input_ids" [1, S] int32 right-padded to the grid S with 179935
54
+ input "attention_mask" [1, S] int32 1 over real tokens, 0 over padding
55
+ output "embedding" [1, 384] fp32 CLS-pooled, L2-normalized
56
+ S = 128 or 512 (export-time choice); batch = 1
57
+ ```
58
+
59
+ **Host recipe** — the tokenizer is the whole contract, and the stock one is not enough:
60
+ - **No prefix, no stripping, no normalization.** Query and document prompts are both empty in
61
+ the checkpoint. Raw whitespace is kept: sentence-transformers strips text before tokenizing,
62
+ the upstream README's `AutoTokenizer` path does not, and the two disagree on `" 東京駅から…\n"`.
63
+ The reference is the raw path.
64
+ - Tokenize with the pinned `tokenizer.json`: regex `Split(Isolated)` → `ByteLevel` (no prefix
65
+ space) → byte BPE with **`ignore_merges = true`** (a whole pre-token that is in the vocabulary
66
+ wins; ` ક` is token 2999, not three). A BPE that ignores the flag tokenizes differently.
67
+ - Truncate the **body to S−2**, then wrap: `[CLS 179934] body… [SEP 179938]`, right-pad with
68
+ **PAD 179935** and mask 0. Truncating after adding the specials loses SEP; padding with 0 is a
69
+ different token. Both are silent.
70
+ - Similarity = dot product (unit vectors). Dimension truncation is not a property of this model.
71
+
72
+ [`conversion/granite_embedding/_granite_tokenizer.py`](https://github.com/john-rocky/coreai-model-zoo/blob/main/conversion/granite_embedding/_granite_tokenizer.py) is that recipe with no HF import, and
73
+ `host/GraniteTokenizer.swift` in this repo the same recipe in Foundation-only Swift; the gate
74
+ holds both to `AutoTokenizer` exactly (ids and masks) over **681 texts × 2 grids = 1,362 cases**
75
+ including every added token in five boundary contexts, and proves four mutations are caught
76
+ (pad 0 / lose SEP / strip / `ignore_merges=false`).
77
+
78
+ ## Measured
79
+
80
+ **iPhone 17 Pro** (iPhone18,1), iOS 27.0 build **24A437**, the compiled `h18p` bundles loaded by
81
+ the native `AIModel` loader, GPU-preferred (MPSGraph/Metal plan). Every row: 35 HF texts, the
82
+ gate below, 105 warm samples, thermal state fair before and after, caches retained (so "first"
83
+ is process-first, not cache-cold). Peak footprint is the whole app process, tokenizer and file
84
+ hashing included. Measured 2026-09-19.
85
+
86
+ | Variant | S | Gate | Min cosine vs HF | Max \|err\| | Load | First after load | **Warm median** | Peak footprint |
87
+ |---|---:|---|---:|---:|---:|---:|---:|---:|
88
+ | fp32 | 128 | 35/35 | 0.999999999999407 | 1.97e-7 | 81 ms | 23.1 ms | **5.54 ms** | 640 MB |
89
+ | fp32 | 512 | 35/35 | 0.999999999999486 | 2.38e-7 | 586 ms | 39.9 ms | **20.99 ms** | 640 MB |
90
+ | w8 / fp32 table | 128 | 35/35 | 0.999410 | 5.67e-3 | 61 ms | 25.3 ms | 6.64 ms | 555 MB |
91
+ | w8 / fp32 table | 512 | 35/35 | 0.999410 | 5.67e-3 | 447 ms | 138.7 ms | 23.07 ms | 553 MB |
92
+
93
+ Each row matched 4/4 retrieval top-1s with 0 clear-pair flips and 0 repeat drift.
94
+
95
+ **Mac** (M4 Max, Mac16,9), macOS 27.0 build 26A428, the JIT `.aimodel`, GPU-preferred, fp32.
96
+ The driver refused to run while any foreign accelerator job was present; 105 warm samples.
97
+
98
+ | S | Gate | Min cosine vs HF | Max \|err\| | Load | First after load | **Warm median** |
99
+ |---:|---|---:|---:|---:|---:|---:|
100
+ | 128 | 35/35 | 0.99999999999967 | 2.98e-7 | 481 ms | 642 ms | **4.14 ms** |
101
+ | 512 | 35/35 | 0.99999999999887 | 2.98e-7 | 470 ms | 264 ms | **4.89 ms** |
102
+
103
+ The Mac h16c AOT twin also passed 70/70 (same numerics), but its timings were taken with another
104
+ lane's GPU evaluation running and are not reported. The w8 variant on Mac is gated on **CPU
105
+ only** (min cosine 0.999410, max |err| 5.67e-3, ranking exact); Mac GPU for w8 was not run.
106
+
107
+ The fixed grid computes every position, so pick the smallest grid that covers the text: S=128
108
+ for queries and short notes, S=512 for passages. **fp32 is the default.** w8 is a storage
109
+ option only — 22% smaller, not faster here — because the 180,000×384 fp32 vocabulary table is
110
+ 276 MB of the bundle and palettization touches the 48 linear weights alone.
111
+
112
+ ## Numerics gate
113
+
114
+ One gate at every stage, the oracle being official HF eager CPU fp32 (transformers 4.57.6):
115
+ per text cosine ≥ 0.999, max element error ≤ 0.02, L2-norm error ≤ 0.002; per query exact
116
+ top-1 over the 12 documents, retrieval-score error ≤ 0.01, and no inversion of any document
117
+ pair the oracle separates by ≥ 0.001; repeat drift ≤ 1e-6. A wrong-pairing control (every vector
118
+ matched to the wrong text) must FAIL.
119
+
120
+ - **Authoring** (`gate_granite_authoring.py`): the re-authored graph against every one of the 13
121
+ saved hidden states, max |err| ≤ **1e-4** at fp32, both grids. Five mutations must trip it:
122
+ all-global, all-local, ignore-padding and mean-pooling fail the embedding gate; a local radius of
123
+ **63 instead of 64** passes the embedding gate (cos 0.99995) and fails only the layer gate —
124
+ which is why the layer gate exists. Whole-model **fp16 fails** this layer gate on both grids.
125
+ - **Export**: the torch-exported, decomposed graph is gated before conversion, on both grids.
126
+ - **Runtime**: Mac CPU and GPU (JIT), Mac h16c AOT, iPhone h18p AOT — the tables above.
127
+ - **w8**: the same gate at prepared, finalized and decomposed stages, 48 `lut_to_dense` ops
128
+ counted, palettes hashed; the iOS w8 export reuses the Mac palettes byte for byte.
129
+
130
+ [`models/granite-embedding-97m/gate-granite-embedding-97m.json`](https://github.com/john-rocky/coreai-model-zoo/blob/main/models/granite-embedding-97m/gate-granite-embedding-97m.json) in the zoo is the transcript: the eight runtime rows
131
+ (min cosine, max error, retrieval, timings, device/OS build), the tokenizer gate and the
132
+ authoring gate, each with the sha256 of the full record it summarizes.
133
+
134
+ ## ⬇️ Bundle
135
+
136
+ This repo — one folder per variant, each self-contained: the bundle, `tokenizer/`, `reference.json` (the
137
+ 35 HF fixtures with ids, masks and embeddings — the parity test) and `provenance/` (export
138
+ manifest with per-file sha256, the runtime gate record). `coreai-kit.json` at the root maps
139
+ platform → folder.
140
+
141
+ | Folder | Platform | Format | Bundle | Bytes |
142
+ |---|---|---|---|---:|
143
+ | `macos/fp32-s512/` **(default)** | macOS 27 | JIT `.aimodel` | `granite97m_fp32_s512_bound.aimodel` | 390,431,506 |
144
+ | `macos/fp32-s128/` | macOS 27 | JIT `.aimodel` | `granite97m_fp32_s128_bound.aimodel` | 389,989,146 |
145
+ | `ios/fp32-s512/` **(default)** | iOS 27, **h18p only** | AOT `.aimodelc` | `granite97m_fp32_s512_bound.h18p.aimodelc` | 390,308,788 |
146
+ | `ios/fp32-s128/` | iOS 27, h18p only | AOT `.aimodelc` | `granite97m_fp32_s128_bound.h18p.aimodelc` | 390,081,410 |
147
+ | `macos/w8-fp32table-s512/` | macOS 27 (CPU-gated) | JIT `.aimodel` | `granite97m_w8_fp32table_s512.aimodel` | 305,569,358 |
148
+ | `macos/w8-fp32table-s128/` | macOS 27 (CPU-gated) | JIT `.aimodel` | `granite97m_w8_fp32table_s128.aimodel` | 305,126,985 |
149
+ | `ios/w8-fp32table-s512/` | iOS 27, h18p only | AOT `.aimodelc` | `granite97m_w8_fp32table_s512_r02.h18p.aimodelc` | 305,479,184 |
150
+ | `ios/w8-fp32table-s128/` | iOS 27, h18p only | AOT `.aimodelc` | `granite97m_w8_fp32table_s128_r02.h18p.aimodelc` | 305,251,774 |
151
+
152
+ The `ios/` bundles are compiled for one device architecture (`h18p`, the iPhone 17 Pro) with
153
+ `xcrun coreai-build compile --platform iOS --min-deployment-version 27.0 --preferred-compute gpu
154
+ --architecture h18p` (coreai-build 3600.83.1). **Never load an iOS bundle on a Mac.** Other
155
+ phones need their own compile from the recipe; the source IR is reproducible, not shipped.
156
+
157
+ Convert yourself: [`conversion/granite_embedding/`](https://github.com/john-rocky/coreai-model-zoo/blob/main/conversion/granite_embedding/README.md)
158
+ — five staged scripts; [`recipe.toml`](https://github.com/john-rocky/coreai-model-zoo/blob/main/models/granite-embedding-97m/recipe.toml) names the commands.
159
+
160
+ ## CoreAIKit (Swift)
161
+
162
+ **Not enrolled** in the kit catalog. The kit's `TextEmbedder` pads with 0, truncates after adding
163
+ the special tokens (losing SEP), applies its own BPE without `ignore_merges`, discovers a single
164
+ `.aimodel`, and has no grid / architecture selection — every one of those is wrong for this
165
+ model. Running it today means: the Swift tokenizer from this repo's `host/` folder, a fixed
166
+ grid, `AIModel` on the platform's folder. Enrolling it needs a `textEmbedding` driver that takes
167
+ the pad id, a SEP-preserving truncation, a per-platform variant path and an AOT-aware loader —
168
+ tracked as maintainer work, not a blocker on the bundle.
169
+
170
+ ## The port in one lesson: gate the layers, not just the vector
171
+
172
+ ModernBERT's alternating local/global attention is the whole risk. The config says
173
+ `local_attention: 128`; the executed window is inclusive `|i − j| ≤ 64` — 129 keys — and a
174
+ window of 63 reproduces the final embedding to cos 0.99995 while every hidden state past layer 1
175
+ is wrong. Only a per-layer oracle catches it. Three more things the raw checkpoint settles that
176
+ the modeling file hides: layer 0 has no attention norm (adding one loads a missing weight),
177
+ the two RoPE thetas are per-layer-kind, and the CLS/L2 head needs an explicit `clamp_min`
178
+ epsilon because the converter's `F.normalize` decomposition drops it.
179
+
180
+ ## License and limits
181
+
182
+ Apache-2.0 at the pinned upstream revision; this repo carries IBM's unmodified card as
183
+ `UPSTREAM_README.md` and a `LICENSE-NOTE.md` listing the changes (static graph, in-graph
184
+ pooling, optional w8 palettes, h18p compile). Not tested: other phones or OS builds, the Mac GPU
185
+ with w8, the Neural Engine, dynamic or batched shapes, S > 512, languages beyond the JA/EN
186
+ fixtures, retrieval quality on a benchmark, sustained thermals, true cache-cold load.
config.json ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "ModernBertModel"
4
+ ],
5
+ "attention_bias": false,
6
+ "attention_dropout": 0.0,
7
+ "bos_token_id": 179934,
8
+ "classifier_activation": "silu",
9
+ "classifier_bias": false,
10
+ "classifier_dropout": 0.0,
11
+ "classifier_pooling": "cls",
12
+ "cls_token_id": 179934,
13
+ "decoder_bias": true,
14
+ "deterministic_flash_attn": false,
15
+ "dtype": "bfloat16",
16
+ "embedding_dropout": 0.0,
17
+ "eos_token_id": 179938,
18
+ "global_attn_every_n_layers": 3,
19
+ "global_rope_theta": 150000.0,
20
+ "gradient_checkpointing": false,
21
+ "hidden_activation": "silu",
22
+ "hidden_size": 384,
23
+ "initializer_cutoff_factor": 2.0,
24
+ "initializer_range": 0.02,
25
+ "intermediate_size": 1536,
26
+ "layer_norm_eps": 1e-05,
27
+ "local_attention": 128,
28
+ "local_rope_theta": 160000.0,
29
+ "max_position_embeddings": 32768,
30
+ "mlp_bias": false,
31
+ "mlp_dropout": 0.0,
32
+ "model_type": "modernbert",
33
+ "norm_bias": false,
34
+ "norm_eps": 1e-05,
35
+ "num_attention_heads": 12,
36
+ "num_hidden_layers": 12,
37
+ "pad_token_id": 179935,
38
+ "position_embedding_type": "absolute",
39
+ "repad_logits_with_grad": false,
40
+ "sep_token_id": 179938,
41
+ "sparse_pred_ignore_index": -100,
42
+ "sparse_prediction": false,
43
+ "transformers_version": "4.56.2",
44
+ "vocab_size": 180000
45
+ }
coreai-kit.json ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "kind": "textEmbedding",
3
+ "model": "ibm-granite/granite-embedding-97m-multilingual-r2",
4
+ "revision": "835ad14087e140460703cf0fae09f97d469d65c2",
5
+ "variants": {
6
+ "macos": {
7
+ "path": "macos/fp32-s512",
8
+ "sizeMB": 396,
9
+ "format": "aimodel"
10
+ },
11
+ "macos-s128": {
12
+ "path": "macos/fp32-s128",
13
+ "sizeMB": 396,
14
+ "format": "aimodel"
15
+ },
16
+ "ios-gpu-h18p": {
17
+ "path": "ios/fp32-s512",
18
+ "sizeMB": 396,
19
+ "format": "aimodelc",
20
+ "architecture": "h18p",
21
+ "minOS": "27.0"
22
+ },
23
+ "ios-gpu-h18p-s128": {
24
+ "path": "ios/fp32-s128",
25
+ "sizeMB": 396,
26
+ "format": "aimodelc",
27
+ "architecture": "h18p",
28
+ "minOS": "27.0"
29
+ },
30
+ "macos-w8": {
31
+ "path": "macos/w8-fp32table-s512",
32
+ "sizeMB": 315,
33
+ "format": "aimodel"
34
+ },
35
+ "ios-gpu-h18p-w8": {
36
+ "path": "ios/w8-fp32table-s512",
37
+ "sizeMB": 315,
38
+ "format": "aimodelc",
39
+ "architecture": "h18p",
40
+ "minOS": "27.0"
41
+ }
42
+ },
43
+ "host": {
44
+ "tokenizer": "host/GraniteTokenizer.swift",
45
+ "pad_id": 179935,
46
+ "cls_id": 179934,
47
+ "sep_id": 179938,
48
+ "body_truncation": "S-2",
49
+ "prefix": "",
50
+ "strip": false,
51
+ "ignore_merges": true
52
+ },
53
+ "graph": {
54
+ "inputs": {
55
+ "input_ids": "[1,S] int32",
56
+ "attention_mask": "[1,S] int32"
57
+ },
58
+ "outputs": {
59
+ "embedding": "[1,384] fp32 L2-normalized"
60
+ },
61
+ "grids": [
62
+ 128,
63
+ 512
64
+ ]
65
+ }
66
+ }