# /// script # requires-python = ">=3.11,<3.13" # dependencies = ["torch==2.9.0", "numpy", "pillow", "coreai-torch==0.4.3", "coreai-core==1.0.0b3", "transformers==5.12.1"] # /// """SigLIP 2 base/16 at 224 (Google, Apache-2.0) → pix-siglip2-base-224.aimodel, its tokenizer and fixtures. uv run Tools/export_siglip2.py [--out Tools/exports] [--dtype float16] Two entry points in one asset: image: pixel_values [1,3,224,224] float32, already resized and normalised ((x/255 - 0.5)/0.5) → embedding [1,768] text: ids [1,64] int32 (lower-cased, Gemma BPE, =1 appended, right-padded with =0) → embedding [1,768] Both are the model's pooled outputs (what get_image_features / get_text_features return); the host normalises to unit length. The tokenizer is written as two flat binary files the Swift side reads without a JSON parse: the vocabulary and the merges as id triples in rank order. Fixtures: texts with their ids and normalised embeddings, and two synthetic images with theirs, so the Swift tokenizer, preprocessing and asset are each checked against transformers.""" import argparse, json, shutil, struct, time from pathlib import Path import numpy as np, torch, torch.nn as nn from PIL import Image from transformers import AutoModel, AutoProcessor, AutoTokenizer import coreai_torch from coreai.runtime import AIModelAssetMetadata HERE = Path(__file__).resolve().parent ap = argparse.ArgumentParser() ap.add_argument("--name", default="google/siglip2-base-patch16-224") ap.add_argument("--revision", default="75de2d55ec2d0b4efc50b3e9ad70dba96a7b2fa2") ap.add_argument("--asset", default="pix-siglip2-base-224.aimodel") ap.add_argument("--dtype", default="float16", choices=["float16", "float32"]) ap.add_argument("--out", default=str(HERE / "exports")) ap.add_argument("--fixtures", default=str(HERE.parent / "Tests/PhotoIndexTests/Fixtures/siglip")) args = ap.parse_args() LENGTH, SIZE = 64, 224 model = AutoModel.from_pretrained(args.name, revision=args.revision, dtype=torch.float32).eval() proc = AutoProcessor.from_pretrained(args.name, revision=args.revision) tok = AutoTokenizer.from_pretrained(args.name, revision=args.revision) class Image_(nn.Module): def __init__(self, m): super().__init__(); self.v = m.vision_model def forward(self, pixel_values): return self.v(pixel_values=pixel_values).pooler_output class Text_(nn.Module): def __init__(self, m): super().__init__(); self.t = m.text_model def forward(self, ids): return self.t(input_ids=ids).pooler_output image_net, text_net = Image_(model).eval(), Text_(model).eval() def ids_for(texts): return tok([t.lower() for t in texts], padding="max_length", max_length=LENGTH, truncation=True, return_tensors="pt")["input_ids"].to(torch.int32) out = Path(args.out); out.mkdir(parents=True, exist_ok=True) support = out / "pix-siglip2-support"; support.mkdir(exist_ok=True) fx = Path(args.fixtures); fx.mkdir(parents=True, exist_ok=True) # Tokenizer → vocab.bin (u32 count, then u32 length + UTF-8 bytes per id) and merges.bin (u32 count, then u32 left, right, merged per rank). tj = json.loads(tok.backend_tokenizer.to_str()) vocab = tj["model"]["vocab"]; inv = sorted(vocab.items(), key=lambda kv: kv[1]) assert [i for _, i in inv] == list(range(len(inv))) with open(support / "vocab.bin", "wb") as f: f.write(struct.pack("=1 appended, right-padded with 0) → embedding [1,768]. Normalise both in the host; cosine ranks.") meta.creation_date = int(time.time()) asset = out / args.asset if asset.exists(): shutil.rmtree(asset) program.save_asset(asset, meta) print(f"saved {asset} ({sum(f.stat().st_size for f in asset.rglob('*'))/1e6:.0f} MB) in {time.time()-t0:.0f} s", flush=True)