Download docs/usage.md from anylab/embeddinggemma-2-coreml: direct link, hf CLI and curl.
- Browser
- Download file 3.75 kB
-
https://huggingface.co/anylab/embeddinggemma-2-coreml/resolve/main/docs/usage.md
- Command line
-
hf download hf://anylab/embeddinggemma-2-coreml/docs/usage.md
-
curl -L -o usage.md https://huggingface.co/anylab/embeddinggemma-2-coreml/resolve/main/docs/usage.md
Using the Core ML package
Use the included stock tokenizer. Prefix queries with
task: search result | query: and documents with title: none | text: ,
replacing none with the actual title when available.
Add BOS token 2 and EOS token 1 exactly once. Choose the smallest fitting length from 128, 256, 512, 1024 and 2048, including the prompt and special tokens. Right-pad with token 0. Inputs longer than 2048 require an application policy; the package does not truncate them automatically.
For function sL, the contract is:
| Feature | Type and shape |
|---|---|
input_ids |
int32 [1,L] |
attention_mask |
float32 [1,L]; real tokens 1, padding 0 |
embedding |
float32 [1,768], L2-normalized |
The masked mean includes prompts and special tokens. Output is the raw model embedding; application head projection and Matryoshka truncation/renormalization are downstream operations.
Python example
On Apple Silicon macOS, install coremltools==9.0, numpy and tokenizers
in a Python 3.12 environment, then run from the parent of the download directory:
from pathlib import Path
import coremltools as ct
import numpy as np
from tokenizers import Tokenizer
root = Path("embeddinggemma-2-coreml")
tokenizer = Tokenizer.from_file(str(root / "tokenizer.json"))
text = "task: search result | query: How do bond prices respond to interest rates?"
ids = tokenizer.encode(text).ids
if not ids or ids[0] != 2:
ids.insert(0, 2)
if ids[-1] != 1:
ids.append(1)
length = next((n for n in (128, 256, 512, 1024, 2048) if len(ids) <= n), None)
if length is None:
raise ValueError("Input exceeds this package's 2048-token limit")
input_ids = np.zeros((1, length), dtype=np.int32)
attention_mask = np.zeros((1, length), dtype=np.float32)
input_ids[0, :len(ids)] = ids
attention_mask[0, :len(ids)] = 1
model = ct.models.MLModel(
str(root / "EmbeddingGemma2Owned.mlpackage"),
function_name=f"s{length}",
compute_units=ct.ComputeUnit.CPU_AND_NE,
)
embedding = model.predict({
"input_ids": input_ids,
"attention_mask": attention_mask,
})["embedding"][0]
assert embedding.shape == (768,) and np.isfinite(embedding).all()
Native inference uses Core ML and does not require Python. See the included Swift validator for a native example.
Loading and memory
Compile the package on the deployment device and retain the compiled artifact in an application-owned directory. Reuse it only for unchanged package bytes. First specialization can take minutes, even though package compilation itself is much shorter.
Load larger functions lazily and explicitly release unused models. Including a function in the download does not load it, but retaining all five can consume about 2 GiB of wired neural memory on the measured M2, beyond ordinary process footprint. Releasing a model may leave reclaimable cache. Full-app 8 GB safety and exact cache-invalidation rules are unmeasured.
cpuAndNeuralEngine permits CPU operations. The measured compute plans place
the transformer core on ANE; this is not an ANE-only execution guarantee.
See measurements and limitations.
Redistributing the model
Include LICENSE, NOTICE and licenses/QVAC-MIT.txt with a model download or
in the application's bundled third-party notices. Keep them available to end
users; a model-only file filter omits them. This applies to the runtime download
as well as a pre-bundled model. Pin notice files independently if the validated
model revision must stay fixed. Evaluation fixtures and their FiQA/CC-BY-SA
notices are for the optional research download; they are not part of the app
or the model-only download. See component licences.