junmo-k commited on
Commit
e97c928
Β·
verified Β·
1 Parent(s): f21079a

Model card: unwrap lines and describe only the current layout

Browse files
Files changed (1) hide show
  1. README.md +27 -102
README.md CHANGED
@@ -17,20 +17,11 @@ tags:
17
 
18
  # EmbeddingGemma 2 for the Apple Neural Engine (text up to 8192 tokens + images)
19
 
20
- Core ML conversion of [google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2) that runs
21
- **entirely on the Apple Neural Engine** (`MLComputeUnits.cpuAndNeuralEngine`, no GPU). It is the model package of
22
- [ANEEmbed](https://github.com/junmo-kim/ANEEmbed) (an open-source Swift embedding server and CLI:
23
- `aneembed models install` downloads this repository at a pinned revision, checks every file against
24
- `manifest.json` and compiles it on your Mac), but it is a plain Core ML package and can be used from any Core ML
25
- client that follows the host contract below and in `manifest.json`.
26
 
27
- Output: the same 768-d L2-normalized embedding as
28
- `SentenceTransformer("google/embeddinggemma-2").encode(...)` (mean pooling over all tokens, then normalization), at
29
- cosine β‰₯ 0.99988 for text up to 8192 tokens and β‰₯ 0.99976 for images in our tests (details below). Matryoshka
30
- truncation (512/256/128) works as in the original: keep the first d values and re-normalize.
31
 
32
- This is the second layout of the package ("layoutR", `manifest_version` 2). The first one ran texts over 2048
33
- tokens layer by layer in 1024-row chunks; this one runs every text in a single call.
34
 
35
  ## Files
36
 
@@ -45,76 +36,37 @@ tokens layer by layer in 1024-row chunks; this one runs every text in a single c
45
  | `selftest.json` + `selftest.bin` | 0.3 MB | golden self-test (16 tests, one per function) to run at load time |
46
  | `LICENSE`, `NOTICE` | | Apache-2.0, attribution |
47
 
48
- Only the `.mlpackage` files are published; compile them on the Mac that runs them (`xcrun coremlcompiler compile
49
- X.mlpackage .` or `MLModel.compileModel(at:)`, about 5 s). `manifest.json` lists the `.mlmodelc` files of the
50
- reference compile (`xcrun coremlcompiler` 3600.25.1).
51
 
52
  Without the weight sharing the same 13 functions are 3.4 GB as separate packages; merged they are 363 MB.
53
 
54
  ## How it was produced
55
 
56
- `tools/convert/build.py` (in the ANEEmbed repository, pinned `requirements.txt`) converts the original
57
- `model.safetensors` (revision `f9972ac22e696a105c482202646979e33e4b5a1e`, sha256 checked) with coremltools 9.0 to
58
- fp16 ML Programs (minimum macOS 15), merges them into one multifunction package (identical weights are stored
59
- once), compiles it with `coremlcompiler`, extracts `embeddings.bf16`, copies the vision package unchanged (sha256
60
- checked against the published files), generates the self-test from fp32 references and writes the manifest. On an
61
- M5 Ultra it takes 5.5 minutes and 16 GB of RAM, CPU only. A second clean build reproduced the weights, the compiled
62
- `model.mil`, the token table and the self-test byte for byte; the `.mlpackage` protobuf is message-equal (its byte
63
- order and the package UUIDs differ between runs).
64
 
65
  The text encoder is re-expressed with numerically neutral rewrites that the ANE needs:
66
 
67
  - RMSNorm divides each row by its max-abs value first (fp16 overflow guard).
68
- - **Attention uses a "fold" layout**: the query heads that share one K/V head are folded into the row dimension.
69
- The usual `repeat_interleave` layout is *miscompiled* by the ANE compiler for the global layers (one K/V head):
70
- rebuilt with it, `winp2048_b512`, `winl4096_b512` and `winl8192_b512` score cosine 0.960, 0.952 and 0.885 on the
71
- ANE against the fp32 reference.
72
- - **Windowed sliding layers.** 20 of the 24 layers only attend 512 tokens to each side. In the windowed functions
73
- they work on blocks of 512 query rows, each against the 1536 key rows around it (static slices clipped to the
74
- sequence), with the mask passed in as a `[S, 1536]` band. Padding the keys inside the graph instead gave the same
75
- numbers but was about 3Γ— slower on the ANE. The 4 global layers attend to every row.
76
- - Masks are runtime inputs and RoPE uses constant tables at absolute positions. Graph-constant masks and a runtime
77
- positions/RoPE input both produced wrong results on the ANE in our tests.
78
- - The per-layer-embedding projection is computed per layer from a slice of its weight, so every function carries
79
- bit-identical weight constants and the merge can share them.
80
 
81
  ## Using it (host contract)
82
 
83
- All functions run with `cpuAndNeuralEngine`. Inputs are fp16, embeddings are fp32. Token rows are
84
- `bf16 β†’ fp32 Γ— √512 β†’ fp16` from `embeddings.bf16`. Text sequences are `[2] + tokenize(prompt + text) + [1]`
85
- (bos 2, eos 1; prompts as in the original, e.g. `title: none | text: `, `task: search result | query: `).
86
- The exact contract (shapes, masks, pooling) is in `manifest.json` β†’ `bucket_contract` and `window`.
87
-
88
- **Up to 1280 tokens: `embed_S`.** Pack one or more sequences back to back into the smallest `embed_S`
89
- (S ∈ 64/128/256/512/768/1024/1280, up to K = max(8, S/32) sequences per call): `inputs_embeds [1,S,512]`;
90
- `global_bias [1,1,S,S]` = 0 where row and column belong to the same sequence (or i = j), else βˆ’10000; `local_bias`
91
- = the same AND |iβˆ’j| ≀ 512; `pool [K,S]` = 1/n_k over the rows of sequence k β†’ `embedding [K,768]` (unused rows are 0).
92
-
93
- **1281 to 2048 tokens: `winp1536_b512` / `winp2048_b512`.** The same as `embed_S` (packing included, K = 48 / 64),
94
- except that `local_bias` is replaced by `band_bias [1,1,S,1536]`: row r, column c is query row r against key row
95
- k = ⌊r/512βŒ‹Β·512 βˆ’ 512 + c; 0 if r and k belong to the same sequence and |r βˆ’ k| ≀ 512, or k = r; else βˆ’10000.
96
- Columns whose key row lies outside [0, S) are never read.
97
-
98
- **2049 to 8192 tokens: `winl{3072,4096,6144,8192}_b512`, one call per text.** S = the smallest size β‰₯ n.
99
- `inputs_embeds [1,S,512]` with rows β‰₯ n zero, `band_bias` as above for one sequence of n rows,
100
- `key_bias [1,1,1,S]` = 0 for k < n else βˆ’10000, `pool [1,S]` β†’ `embedding [1,768]`. **`pool` must be 2⁻¹⁰ on the n
101
- valid rows, not 1/n** (1/n underflows fp16 inside the ANE pooling at large n; the scale cancels in the
102
- normalization).
103
-
104
- **Images.** Preprocess exactly like the HF `Gemma4ImageProcessor` (aspect-preserving bicubic resize to multiples of
105
- 48 px within 9 Γ— budget patches, pixels / 255, 16 Γ— 16 patches), run `vision_{70,140,280}` (positions, position
106
- embeddings from `position_embeddings.f16`, validity and 3 Γ— 3 pooling matrix as in `manifest.json` β†’ `vision`),
107
- then embed `<bos> <|image> [image tokens] <image|> <eos>` with the packing functions like a text sequence; images
108
- and texts can share one call.
109
 
110
  ## Accuracy
111
 
112
- Cosine similarity against the **official** model: google/embeddinggemma-2 in fp32 (our PyTorch reference equals the
113
- official transformers / sentence-transformers fp32 stack at cosine 1.0000000 for texts of 64 to 8192 tokens), all
114
- functions on `cpuAndNeuralEngine`, Apple M5 Ultra, macOS 27.0.1. Public data only: multilingual READMEs
115
- (en/ko/ja/zh/fr) of an Apache-2.0 project, the Korean and Japanese constitutions (public domain), MIT-licensed
116
- technical docs and Apache-2.0 source code; images bundled with scikit-learn / networkx / llama.cpp /
117
- stable-diffusion.cpp plus synthetic images.
118
 
119
  | Category | n | min | median |
120
  |---|---:|---:|---:|
@@ -133,9 +85,7 @@ stable-diffusion.cpp plus synthetic images.
133
  | Image, budget 280, alone | 13 | 0.999954 | 0.999979 |
134
  | Images packed with other images and a text (1024 / 2048) | 91 | 0.999769 | 0.999975 |
135
 
136
- The long-text lengths include the first token count of every size (2049, 3073, 4097, 6145) and the last (3072, 4096,
137
- 6144, 8192). The image error comes almost entirely from the fp16 vision encoder: feeding the official fp32 image
138
- tokens through the same ANE text functions gives β‰₯ 0.999981.
139
 
140
  ## Speed (Apple M5 Ultra, macOS 27.0.1)
141
 
@@ -151,48 +101,23 @@ Median latency of one call, one text filling it, through ANEEmbed's Swift runtim
151
  | `embed_1024` | 70.7 ms | `winl8192_b512` | 1168.7 ms |
152
  | `embed_1280` | 93.5 ms | | |
153
 
154
- First load per model path, executable and macOS build triggers the ANE compiler (the OS caches the result):
155
- the 9 packing functions plus vision took 236 s through ANEEmbed, the 4 long functions another 383 s; with the
156
- compile cached, loading all of them and running the self-test takes 4.9 s (2048 tokens) / 9.1 s (8192 tokens).
157
- Load only the functions up to the length you serve.
158
 
159
  ## Memory (Apple M5 Ultra, macOS 27.0.1, through ANEEmbed)
160
 
161
- Core ML gives each loaded function working memory for the Neural Engine, owned by the loading process but outside
162
- its `phys_footprint` (`footprint -w` lists it as `Owned physical footprint (unmapped) (neural)`): 5.25 GB for the 9
163
- packing functions plus the vision functions, 13.3 GB with the 4 long functions added. It is wired only while
164
- used: all of it for about 60 s after the functions first run, then nothing while idle (purgeable: under memory
165
- pressure the OS discarded 1.6 GB of it, and the next calls recreated it), and during a call plus about 10 s only
166
- the buffers of the called function (266 MB for a short query, 579 MB for a 1617-token text, 3.9 GB for a
167
- 6631-token text in `winl8192_b512`). The first layout of this package needed 21.5 GB for its long-text functions,
168
- and a 6631-token text wired 14.3 GB. Load only the functions up to the length you serve.
169
 
170
  ## Self-test (run it at load)
171
 
172
- The ANE compiler output depends on the OS version, and a miscompile does not raise an error, it returns wrong
173
- numbers. `selftest.json` / `selftest.bin` contain one deterministic test per function (16 tests): text inputs are
174
- built from token ids stored in the JSON (from this package's `LICENSE` text) and the shipped table, image inputs
175
- from a procedural integer pattern; expected fp32 outputs come from the fp32 reference (official fp32 for the vision
176
- tests). Text tests compare embeddings by cosine (packing tests also require unused rows to stay 0); vision tests
177
- compare an 8-column random projection (digest) of the image tokens by cosine and relative error. Recommended
178
- tolerance: cosine β‰₯ 0.999, relative error ≀ 0.05; measured on M5 Ultra: cosine β‰₯ 0.99990 for every function,
179
- relative error ≀ 0.015 (vision). The repeat-layout builds above fail it on the ANE (0.885–0.960). Reference runner:
180
- `tools/convert/selftest.py run <dir>`.
181
 
182
  ## Limitations
183
 
184
- - Apple Neural Engine only. The package was tuned and verified with `cpuAndNeuralEngine`; other compute units are
185
- not tested.
186
- - Text longer than 8192 tokens must be split by the caller. Images use the HF preprocessing; llama.cpp's image
187
- preprocessing differs, so image embeddings are not interchangeable with llama.cpp GGUF ones.
188
  - Audio and video are not included in this package.
189
- - First load per model path compiles for the ANE (about 10 minutes for all text functions on M5 Ultra); move the
190
- directory, update macOS or run it from another executable and it compiles again.
191
 
192
  ## License and attribution
193
 
194
- Apache-2.0, see `LICENSE` and `NOTICE`. Original model: EmbeddingGemma 2 by Google DeepMind
195
- ([google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2)); use must also follow the
196
- [Gemma Prohibited Use Policy](https://ai.google.dev/gemma/prohibited_use_policy). Vision encoder conversion and
197
- position tables: [FluidInference/embeddinggemma-2-coreml](https://huggingface.co/FluidInference/embeddinggemma-2-coreml)
198
- (Apache-2.0), unchanged.
 
17
 
18
  # EmbeddingGemma 2 for the Apple Neural Engine (text up to 8192 tokens + images)
19
 
20
+ Core ML conversion of [google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2) that runs **entirely on the Apple Neural Engine** (`MLComputeUnits.cpuAndNeuralEngine`, no GPU). It is the model package of [ANEEmbed](https://github.com/junmo-kim/ANEEmbed) (an open-source Swift embedding server and CLI: `aneembed models install` downloads this repository at a pinned revision, checks every file against `manifest.json` and compiles it on your Mac), but it is a plain Core ML package and can be used from any Core ML client that follows the host contract below and in `manifest.json`.
 
 
 
 
 
21
 
22
+ Output: the same 768-d L2-normalized embedding as `SentenceTransformer("google/embeddinggemma-2").encode(...)` (mean pooling over all tokens, then normalization), at cosine β‰₯ 0.99988 for text up to 8192 tokens and β‰₯ 0.99976 for images in our tests (details below). Matryoshka truncation (512/256/128) works as in the original: keep the first d values and re-normalize.
 
 
 
23
 
24
+ Package layout `manifest_version` 2: every text up to 8192 tokens runs in a single Neural Engine call.
 
25
 
26
  ## Files
27
 
 
36
  | `selftest.json` + `selftest.bin` | 0.3 MB | golden self-test (16 tests, one per function) to run at load time |
37
  | `LICENSE`, `NOTICE` | | Apache-2.0, attribution |
38
 
39
+ Only the `.mlpackage` files are published; compile them on the Mac that runs them (`xcrun coremlcompiler compile X.mlpackage .` or `MLModel.compileModel(at:)`, about 5 s). `manifest.json` lists the `.mlmodelc` files of the reference compile (`xcrun coremlcompiler` 3600.25.1).
 
 
40
 
41
  Without the weight sharing the same 13 functions are 3.4 GB as separate packages; merged they are 363 MB.
42
 
43
  ## How it was produced
44
 
45
+ `tools/convert/build.py` (in the ANEEmbed repository, pinned `requirements.txt`) converts the original `model.safetensors` (revision `f9972ac22e696a105c482202646979e33e4b5a1e`, sha256 checked) with coremltools 9.0 to fp16 ML Programs (minimum macOS 15), merges them into one multifunction package (identical weights are stored once), compiles it with `coremlcompiler`, extracts `embeddings.bf16`, copies the vision package unchanged (sha256 checked against the published files), generates the self-test from fp32 references and writes the manifest. On an M5 Ultra it takes 5.5 minutes and 16 GB of RAM, CPU only. A second clean build reproduced the weights, the compiled `model.mil`, the token table and the self-test byte for byte; the `.mlpackage` protobuf is message-equal (its byte order and the package UUIDs differ between runs).
 
 
 
 
 
 
 
46
 
47
  The text encoder is re-expressed with numerically neutral rewrites that the ANE needs:
48
 
49
  - RMSNorm divides each row by its max-abs value first (fp16 overflow guard).
50
+ - **Attention uses a "fold" layout**: the query heads that share one K/V head are folded into the row dimension. The usual `repeat_interleave` layout is *miscompiled* by the ANE compiler for the global layers (one K/V head): rebuilt with it, `winp2048_b512`, `winl4096_b512` and `winl8192_b512` score cosine 0.960, 0.952 and 0.885 on the ANE against the fp32 reference.
51
+ - **Windowed sliding layers.** 20 of the 24 layers only attend 512 tokens to each side. In the windowed functions they work on blocks of 512 query rows, each against the 1536 key rows around it (static slices clipped to the sequence), with the mask passed in as a `[S, 1536]` band. Padding the keys inside the graph instead gave the same numbers but was about 3Γ— slower on the ANE. The 4 global layers attend to every row.
52
+ - Masks are runtime inputs and RoPE uses constant tables at absolute positions. Graph-constant masks and a runtime positions/RoPE input both produced wrong results on the ANE in our tests.
53
+ - The per-layer-embedding projection is computed per layer from a slice of its weight, so every function carries bit-identical weight constants and the merge can share them.
 
 
 
 
 
 
 
 
54
 
55
  ## Using it (host contract)
56
 
57
+ All functions run with `cpuAndNeuralEngine`. Inputs are fp16, embeddings are fp32. Token rows are `bf16 β†’ fp32 Γ— √512 β†’ fp16` from `embeddings.bf16`. Text sequences are `[2] + tokenize(prompt + text) + [1]` (bos 2, eos 1; prompts as in the original, e.g. `title: none | text: `, `task: search result | query: `). The exact contract (shapes, masks, pooling) is in `manifest.json` β†’ `bucket_contract` and `window`.
58
+
59
+ **Up to 1280 tokens: `embed_S`.** Pack one or more sequences back to back into the smallest `embed_S` (S ∈ 64/128/256/512/768/1024/1280, up to K = max(8, S/32) sequences per call): `inputs_embeds [1,S,512]`; `global_bias [1,1,S,S]` = 0 where row and column belong to the same sequence (or i = j), else βˆ’10000; `local_bias` = the same AND |iβˆ’j| ≀ 512; `pool [K,S]` = 1/n_k over the rows of sequence k β†’ `embedding [K,768]` (unused rows are 0).
60
+
61
+ **1281 to 2048 tokens: `winp1536_b512` / `winp2048_b512`.** The same as `embed_S` (packing included, K = 48 / 64), except that `local_bias` is replaced by `band_bias [1,1,S,1536]`: row r, column c is query row r against key row k = ⌊r/512βŒ‹Β·512 βˆ’ 512 + c; 0 if r and k belong to the same sequence and |r βˆ’ k| ≀ 512, or k = r; else βˆ’10000. Columns whose key row lies outside [0, S) are never read.
62
+
63
+ **2049 to 8192 tokens: `winl{3072,4096,6144,8192}_b512`, one call per text.** S = the smallest size β‰₯ n. `inputs_embeds [1,S,512]` with rows β‰₯ n zero, `band_bias` as above for one sequence of n rows, `key_bias [1,1,1,S]` = 0 for k < n else βˆ’10000, `pool [1,S]` β†’ `embedding [1,768]`. **`pool` must be 2⁻¹⁰ on the n valid rows, not 1/n** (1/n underflows fp16 inside the ANE pooling at large n; the scale cancels in the normalization).
64
+
65
+ **Images.** Preprocess exactly like the HF `Gemma4ImageProcessor` (aspect-preserving bicubic resize to multiples of 48 px within 9 Γ— budget patches, pixels / 255, 16 Γ— 16 patches), run `vision_{70,140,280}` (positions, position embeddings from `position_embeddings.f16`, validity and 3 Γ— 3 pooling matrix as in `manifest.json` β†’ `vision`), then embed `<bos> <|image> [image tokens] <image|> <eos>` with the packing functions like a text sequence; images and texts can share one call.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
  ## Accuracy
68
 
69
+ Cosine similarity against the **official** model: google/embeddinggemma-2 in fp32 (our PyTorch reference equals the official transformers / sentence-transformers fp32 stack at cosine 1.0000000 for texts of 64 to 8192 tokens), all functions on `cpuAndNeuralEngine`, Apple M5 Ultra, macOS 27.0.1. Public data only: multilingual READMEs (en/ko/ja/zh/fr) of an Apache-2.0 project, the Korean and Japanese constitutions (public domain), MIT-licensed technical docs and Apache-2.0 source code; images bundled with scikit-learn / networkx / llama.cpp / stable-diffusion.cpp plus synthetic images.
 
 
 
 
 
70
 
71
  | Category | n | min | median |
72
  |---|---:|---:|---:|
 
85
  | Image, budget 280, alone | 13 | 0.999954 | 0.999979 |
86
  | Images packed with other images and a text (1024 / 2048) | 91 | 0.999769 | 0.999975 |
87
 
88
+ The long-text lengths include the first token count of every size (2049, 3073, 4097, 6145) and the last (3072, 4096, 6144, 8192). The image error comes almost entirely from the fp16 vision encoder: feeding the official fp32 image tokens through the same ANE text functions gives β‰₯ 0.999981.
 
 
89
 
90
  ## Speed (Apple M5 Ultra, macOS 27.0.1)
91
 
 
101
  | `embed_1024` | 70.7 ms | `winl8192_b512` | 1168.7 ms |
102
  | `embed_1280` | 93.5 ms | | |
103
 
104
+ First load per model path, executable and macOS build triggers the ANE compiler (the OS caches the result): the 9 packing functions plus vision took 236 s through ANEEmbed, the 4 long functions another 383 s; with the compile cached, loading all of them and running the self-test takes 4.9 s (2048 tokens) / 9.1 s (8192 tokens). Load only the functions up to the length you serve.
 
 
 
105
 
106
  ## Memory (Apple M5 Ultra, macOS 27.0.1, through ANEEmbed)
107
 
108
+ Core ML gives each loaded function working memory for the Neural Engine, owned by the loading process but outside its `phys_footprint` (`footprint -w` lists it as `Owned physical footprint (unmapped) (neural)`): 5.25 GB for the 9 packing functions plus the vision functions, 13.3 GB with the 4 long functions added. It is wired only while used: all of it for about 60 s after the functions first run, then nothing while idle (purgeable: under memory pressure the OS discarded 1.6 GB of it, and the next calls recreated it), and during a call plus about 10 s only the buffers of the called function (266 MB for a short query, 579 MB for a 1617-token text, 3.9 GB for a 6631-token text in `winl8192_b512`). Load only the functions up to the length you serve.
 
 
 
 
 
 
 
109
 
110
  ## Self-test (run it at load)
111
 
112
+ The ANE compiler output depends on the OS version, and a miscompile does not raise an error, it returns wrong numbers. `selftest.json` / `selftest.bin` contain one deterministic test per function (16 tests): text inputs are built from token ids stored in the JSON (from this package's `LICENSE` text) and the shipped table, image inputs from a procedural integer pattern; expected fp32 outputs come from the fp32 reference (official fp32 for the vision tests). Text tests compare embeddings by cosine (packing tests also require unused rows to stay 0); vision tests compare an 8-column random projection (digest) of the image tokens by cosine and relative error. Recommended tolerance: cosine β‰₯ 0.999, relative error ≀ 0.05; measured on M5 Ultra: cosine β‰₯ 0.99990 for every function, relative error ≀ 0.015 (vision). The repeat-layout builds above fail it on the ANE (0.885–0.960). Reference runner: `tools/convert/selftest.py run <dir>`.
 
 
 
 
 
 
 
 
113
 
114
  ## Limitations
115
 
116
+ - Apple Neural Engine only. The package was tuned and verified with `cpuAndNeuralEngine`; other compute units are not tested.
117
+ - Text longer than 8192 tokens must be split by the caller. Images use the HF preprocessing; llama.cpp's image preprocessing differs, so image embeddings are not interchangeable with llama.cpp GGUF ones.
 
 
118
  - Audio and video are not included in this package.
119
+ - First load per model path compiles for the ANE (about 10 minutes for all text functions on M5 Ultra); move the directory, update macOS or run it from another executable and it compiles again.
 
120
 
121
  ## License and attribution
122
 
123
+ Apache-2.0, see `LICENSE` and `NOTICE`. Original model: EmbeddingGemma 2 by Google DeepMind ([google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2)); use must also follow the [Gemma Prohibited Use Policy](https://ai.google.dev/gemma/prohibited_use_policy). Vision encoder conversion and position tables: [FluidInference/embeddinggemma-2-coreml](https://huggingface.co/FluidInference/embeddinggemma-2-coreml) (Apache-2.0), unchanged.