alexwengg commited on
Commit
f9d567d
·
verified ·
1 Parent(s): c0fefd0

Add images: EmbeddingGemma2Vision (vision_70/140/280, GPU fp16) + position tables

Browse files
.gitattributes CHANGED
@@ -35,3 +35,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  embeddings.bf16 filter=lfs diff=lfs merge=lfs -text
37
  tokenizer.json filter=lfs diff=lfs merge=lfs -text
 
 
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
  embeddings.bf16 filter=lfs diff=lfs merge=lfs -text
37
  tokenizer.json filter=lfs diff=lfs merge=lfs -text
38
+ position_embeddings.f16 filter=lfs diff=lfs merge=lfs -text
EmbeddingGemma2Vision.mlpackage/Data/com.apple.CoreML/model.mlmodel ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0085b5e04a13d000fe5969adeaa13d897f5a3cb7be60c76c8685c023747f89ef
3
+ size 2053174
EmbeddingGemma2Vision.mlpackage/Data/com.apple.CoreML/weights/weight.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5b5b6dd8d80aa438bf91ba143fbe90d56a2e25bfcf685a56b990aad71cf75232
3
+ size 304078400
EmbeddingGemma2Vision.mlpackage/Manifest.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "fileFormatVersion": "1.0.0",
3
+ "itemInfoEntries": {
4
+ "3384970F-23B1-4A78-86B7-B8602D572C97": {
5
+ "author": "com.apple.CoreML",
6
+ "description": "CoreML Model Weights",
7
+ "name": "weights",
8
+ "path": "com.apple.CoreML/weights"
9
+ },
10
+ "5D7ED9C5-95C5-45EC-A0E2-0599BE72866F": {
11
+ "author": "com.apple.CoreML",
12
+ "description": "CoreML Model Specification",
13
+ "name": "model.mlmodel",
14
+ "path": "com.apple.CoreML/model.mlmodel"
15
+ }
16
+ },
17
+ "rootModelIdentifier": "5D7ED9C5-95C5-45EC-A0E2-0599BE72866F"
18
+ }
README.md CHANGED
@@ -14,11 +14,11 @@ tags:
14
  - gemma
15
  ---
16
 
17
- # EmbeddingGemma 2 (text + audio) — Core ML
18
 
19
- The text and audio encoders of [google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2) converted to
20
- Core ML: text runs entirely on the Apple Neural Engine, audio on the GPU. Weights are the original ones (stored fp16 in the model; the token table is
21
- the original bf16). The audio encoder is included (see Audio); the vision encoder is not.
22
 
23
  Output: the same 768-d, L2-normalized embedding as `SentenceTransformer("google/embeddinggemma-2").encode(...)`
24
  (mean pooling over tokens, then normalization). Matryoshka truncation to 512/256/128 works as in the original: keep
@@ -35,7 +35,9 @@ space; the text functions then embed them, so audio and text share one space (se
35
  | `embeddings.bf16` | token embedding table, 262,144 × 512 raw bfloat16 (256 MB); look up rows, multiply by √512 |
36
  | `tokenizer.json` | the original Gemma tokenizer, unchanged |
37
  | `EmbeddingGemma2Audio.mlpackage` | audio encoder (Gemma 4 USM conformer, 12 layers) + projection, fp16, log-mel in-graph (561 MB) |
38
- | `config.json` | shapes, function list, token ids, task prefixes, audio window layout |
 
 
39
 
40
  ## Functions
41
 
@@ -73,6 +75,26 @@ top-5 overlap was 95%.
73
  | audio model + text model, one window after another | 10.2 s (353×) |
74
  | fp32 audio model (exact to PyTorch, token cos 1.0000) | 13.2 s (272×) |
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  ## Use
77
 
78
  1. Prepend the task prefix (e.g. `title: none | text: ` for documents, `task: search result | query: ` for queries;
 
14
  - gemma
15
  ---
16
 
17
+ # EmbeddingGemma 2 (text + audio + image) — Core ML
18
 
19
+ The text, audio and vision encoders of [google/embeddinggemma-2](https://huggingface.co/google/embeddinggemma-2)
20
+ converted to Core ML: text runs entirely on the Apple Neural Engine, audio and images on the GPU. Weights are the original ones (stored fp16 in the model; the token table is
21
+ the original bf16). All three encoders are included (see Audio and Images); video uses the image encoder on frames.
22
 
23
  Output: the same 768-d, L2-normalized embedding as `SentenceTransformer("google/embeddinggemma-2").encode(...)`
24
  (mean pooling over tokens, then normalization). Matryoshka truncation to 512/256/128 works as in the original: keep
 
35
  | `embeddings.bf16` | token embedding table, 262,144 × 512 raw bfloat16 (256 MB); look up rows, multiply by √512 |
36
  | `tokenizer.json` | the original Gemma tokenizer, unchanged |
37
  | `EmbeddingGemma2Audio.mlpackage` | audio encoder (Gemma 4 USM conformer, 12 layers) + projection, fp16, log-mel in-graph (561 MB) |
38
+ | `EmbeddingGemma2Vision.mlpackage` | vision encoder (Gemma 4 ViT, 16 layers, 2-D RoPE) + 3x3 pooling + projection, fp16, functions `vision_70` / `vision_140` / `vision_280` (292 MB) |
39
+ | `position_embeddings.f16` | the vision patch embedder's x / y position tables, rows 0–1023, fp16 (3 MB) |
40
+ | `config.json` | shapes, function list, token ids, task prefixes, audio window and image patch layout |
41
 
42
  ## Functions
43
 
 
75
  | audio model + text model, one window after another | 10.2 s (353×) |
76
  | fp32 audio model (exact to PyTorch, token cos 1.0000) | 13.2 s (272×) |
77
 
78
+ ## Images
79
+
80
+ `EmbeddingGemma2Vision.mlpackage` has one fixed-shape function per soft-token budget: `vision_70` (630 patches),
81
+ `vision_140` (1,260) and `vision_280` (2,520, the model's default). Resize the image keeping its aspect ratio so the
82
+ sides are multiples of 48 px and it has at most 9 × budget patches of 16 px (HF `Gemma4ImageProcessor`), scale RGB to
83
+ [0, 1], cut it into patches row by row (each patch `[16 rows][16 cols][RGB]`) and pad to the function's patch count.
84
+ Inputs: `patches` [1, N, 768], `positions` [1, N, 2] (x, y; −1 for padding), `position_embeddings` [1, N, 768]
85
+ (`table[0][x] + table[1][y]` from `position_embeddings.f16`), `valid` [1, N] and `pool` [T, N] (1/9 for each patch in
86
+ its 3 × 3 group). Output `image_tokens` [1, T, 512]; embed `<bos> <|image> tokens <image|> <eos>` with the text model.
87
+
88
+ | Budget | Zero-shot Oxford Pets (370 photos, 37 breeds) | GPU, M5 Pro | Neural Engine |
89
+ |---|---|---|---|
90
+ | 70 tokens | 87.0% | 14.6 ms (68 images/s) | 37 ms |
91
+ | 140 tokens | 88.4% | 34 ms (30 images/s) | 102 ms |
92
+ | 280 tokens | 89.5% | 64–177 ms | 341 ms |
93
+
94
+ Every op runs on the Neural Engine, but its time grows with the square of the patch count (full attention over all
95
+ patches), so the GPU is faster at every budget. fp32 wrapper vs sentence-transformers: cosine 1.000000; fp16 token
96
+ cosine ≥ 0.997 at 70/140 tokens.
97
+
98
  ## Use
99
 
100
  1. Prepend the task prefix (e.g. `title: none | text: ` for documents, `task: search result | query: ` for queries;
config.json CHANGED
@@ -3,7 +3,8 @@
3
  "format": "coreml",
4
  "modality": [
5
  "text",
6
- "audio"
 
7
  ],
8
  "precision": "fp16",
9
  "hidden_size": 512,
@@ -75,5 +76,42 @@
75
  "boa_token_id": 256000,
76
  "eoa_token_id": 258883,
77
  "text_sequence": "<bos> <|audio> [audio tokens] <audio|> <eos> -> embed_256"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
78
  }
79
  }
 
3
  "format": "coreml",
4
  "modality": [
5
  "text",
6
+ "audio",
7
+ "image"
8
  ],
9
  "precision": "fp16",
10
  "hidden_size": 512,
 
76
  "boa_token_id": 256000,
77
  "eoa_token_id": 258883,
78
  "text_sequence": "<bos> <|audio> [audio tokens] <audio|> <eos> -> embed_256"
79
+ },
80
+ "vision": {
81
+ "package": "EmbeddingGemma2Vision.mlpackage",
82
+ "compute_units": "cpuAndGPU",
83
+ "precision": "fp16",
84
+ "functions": {
85
+ "vision_70": {
86
+ "tokens": 70,
87
+ "patches": 630
88
+ },
89
+ "vision_140": {
90
+ "tokens": 140,
91
+ "patches": 1260
92
+ },
93
+ "vision_280": {
94
+ "tokens": 280,
95
+ "patches": 2520
96
+ }
97
+ },
98
+ "patch_size": 16,
99
+ "pooling_kernel": 3,
100
+ "resize": "aspect-preserving, bicubic + antialias, sides multiple of 48 px, at most 9 x tokens patches",
101
+ "pixel_scale": "RGB / 255 (the model applies 2x - 1)",
102
+ "patch_layout": "row-major patches, each [16 rows][16 cols][RGB]",
103
+ "position_table": {
104
+ "file": "position_embeddings.f16",
105
+ "dtype": "float16",
106
+ "shape": [
107
+ 2,
108
+ 1024,
109
+ 768
110
+ ],
111
+ "use": "table[0][x] + table[1][y] per patch, zero for padding"
112
+ },
113
+ "boi_token_id": 255999,
114
+ "eoi_token_id": 258882,
115
+ "text_sequence": "<bos> <|image> [image tokens] <image|> <eos> -> smallest embed_S that fits"
116
  }
117
  }
position_embeddings.f16 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:17de9480acc23638c0634a97ed89e51ed664433256d7d6f109587dcd91c79124
3
+ size 3145728