Repository navigation
Add EmbeddingGemma 2 full support - #684
Conversation
|
Thanks for this, the results look great! One request: |
I can change them to package, but there is a reason why I opened them public: if in the future someone wants to built its own let's say embedding Gemma 3 or whatever on the architecture and have it closed in its own app without sharing it here, he would be able to do it... @davidkoski let me know if you want them package, or if we want to leave more freedom for the clients of the library to use them... |
|
Re: Right now you have the model name as part of each of the types, e.g. One last thought: I think it is ok to try some things out. We can make them My inclination would be to make them package, but I don't think it is a hard requirement. Since this is open source we can be a little more malleable than yearly binary releases. There isn't a clear winner in the approach, just some potential tradeoffs each way. |
|
Thanks for this — we've been evaluating EmbeddingGemma 2 for on-device search in an iOS/macOS app and ran your branch as a text-only user, in case the data helps. Setup: text tower only — Parity (macOS): against transformers running on the same dequantized weights, cosine min 0.9998 / mean 0.9999 on short queries and 512-token documents; 0.99997 on 1k/2k/4k-token inputs (beyond the sliding window); a right-padded batch (3 / 15 / 512 / 4000 tokens) matches one-by-one runs at ≥ 0.99994. iPhone 14 Pro (6 GB): load 0.59 s, ~208 s per 1,000 documents at 512 tokens, peak footprint ~391 MB. On a small Polish/English retrieval set (31 docs, 32 queries), MRR 0.94, cross-language 0.90 — identical to the full-precision model in sentence-transformers. Three things a text-only user runs into:
AI disclosure: the measurements and this summary were prepared with an AI coding assistant (Claude); I reviewed the method and the results before posting. |
|
@davidkoski, regarding the text-only loading issue @rurza reported, would it make sense to merge #580 first, then adopt its component-loading infrastructure for EmbeddingGemma 2’s vision and audio towers? |
3b5f26c to
7cd9b8b
Compare
Adopted the suggested approach. Rebased on latest main as well. |
|
Yeah, I will review 580 next. |
|
#580 is merged |
Support text, images, video, audio, and interleaved embeddings. Load vision and audio on first use through the shared component loader, preserving an immutable model tree and keeping text-only checkpoints usable without processor files. Keep shared encoder implementations package-scoped. Add reference, component lifecycle, and real-checkpoint integration tests.
7cd9b8b to
a033373
Compare
davidkoski
left a comment
There was a problem hiding this comment.
This looks great, thank you!
…e space as text (part of #836) (#846) * test(embedder): mlx-vlm reference vectors for twelve generated images and their captions (#836) The reference generator renders twelve drawings (Pillow, deterministic, ours to commit), runs them through mlx-vlm 3d87e884's vision tower and stores the 768 and 512 vectors, a query and a caption text case per image, and text-to-image ranking checks. The text rows are byte-identical to the committed ones (mlx pinned to 0.32.3 in the uv command; 0.32.4 shifts them by 1e-3). The weight-key fixture gains the 211 vision keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> TDD-Skip: fixture-generator script (Pillow drawings); its output is pinned by EmbeddingGemma2ReferenceTests * feat(embedder): EmbeddingGemma 2 vision encoder, image path and embedImage (#836) Ports the Gemma 4 vision tower, the multimodal projection and the image processor's sizing and bicubic resampling from ml-explore/mlx-swift-lm#684 (credited in the headers; the pin stays on 3.32.x). An image is <bos><boi><image>xn<eoi><eos> with no prompt text (the model card: prefixes are for text), under its own image-prompt-v1 in imageFingerprint. The tower loads lazily on the first image and drops on a memory warning. The text core is split into scaledEmbeddings + forward so soft tokens can be substituted between them; the text fingerprint is pinned as a literal. Numeric parity is the in-app stage's, next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * feat(selftest): EG2REF checks image vectors against mlx-vlm and measures RAM, time and the floor inputs (#836) The stage now runs the fixture's twelve images through embedImage: cosine >= 0.999 at 768 and 512, the relevance order and top-1, per-image time and memory, drop-and-reload, the text-to-image cosine distributions against text-to-text, the mixed text+image crowding check and what the text floors would admit. A SHA-256 over the text vectors proves the text path is bit-identical across builds. The vision encoder evaluates one layer at a time: MLX peak 1,309 MB -> 832 MB for ~30 ms an image. Parity measured in-app: min cosine 0.99993, top-1 12/12. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> TDD-Skip: in-app SelfTest stage (metallib wall keeps it out of swift test); verified by running it, output in docs/evals/2026-11-15-eg2-vision * docs(evals): EmbeddingGemma 2 vision parity, RAM, time and the text-to-image floor inputs; a proposed floor, unsigned (#836) Measurements from the in-app reference stage on an M1 Max: image cosine 0.99993 vs mlx-vlm, top-1 12/12, +320 MB resident for the tower, ~270 ms an image, text digest bit-identical to master. The text-to-image dead zone is 0.681 to 0.707 on twelve drawings; 0.69 is PROPOSED for challenger and is not signed. EmbedderFloors untouched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(embedder): vision path review fold: guarded MLX reads, Pillow resample order, one resample, image-v2 (#836) Guarded loads and forward pass (an MLX error throws visionFailed), distinct vision errors, a weak latest-target memory-pressure watcher registered before the throwing steps and cancelled on deinit, Pillow's horizontal-then-vertical rounded resample with a full-size decode (8,192 px cap, no thumbnail filter), a cap on the weight allocation for extreme aspect ratios, image-v2 covering preprocessing, decode tests (alpha, gray, 1x1, tiny, EXIF, corrupt), a photo-sized noisy fixture checked in-app at 0.99995, a NOTICE line for the ported MIT code, README divergences. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(embedder): log a refused vision tower under the catalogued mlx-load category (#836) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(evals): EG2REF output from the final tree (image-v2, noisy frame) (#836) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Proposed changes
EmbeddingGemma 2 maps text, images, video and audio into one embedding space, and one input can interleave them. It is a bidirectional encoder, not a chat model, so the VLM factory cannot load it.
From my personal point of view: the best embedding model I have tried in general!
Checked against the Transformers default SDPA path, the one Sentence Transformers uses: cosine 0.9999 for audio, video and interleaved inputs, and 0.99995 at 2,880 and 7,490 tokens.
Checklist
Put an
xin the boxes that apply.pre-commit run --all-filesto format my code / installed pre-commit prior to committing changesAI usage
accurately describes the code changes.