Skip to content
Other

MobileCLIP2-S4 (ONNX)

Apple model export for fast image recognition

2 min readupdated

This model is a conversion of Apple's original multimodal MobileCLIP2-S4 development into the ONNX format. It performs zero-shot image classification (classifying images without prior training on those classes) and processes text and visual prompts separately. The output matches probabilities for given text labels and an image using ONNX Runtime. Based on the FastViT architecture with re-parameterized branches for inference, it was released by author AX1Y2JP. Compared to the original versions, this release features optimized graphs, fp16 and int8 weights for faster performance, and a adjusted tokenizer to preserve recognition accuracy.

Key features

  • Exported to ONNX format (opset 23) for use with ONNX Runtime 1.22+
  • Separate graphs for visual and text encoders
  • FastViT encoder re-parameterization with merged branches for fast inference
  • Optimized options available with fp16 weights and 8-bit quantization (MatMulNBits)
  • Preserved logit_scale parameter in model metadata

What you can do

  • Integrate the model into Python applications using onnxruntime for image classification
  • Perform image classification using custom text labels without fine-tuning
  • Run inference on GPU (CUDAExecutionProvider) or CPU to speed up processing

Example

import numpy as np
import onnxruntime as ort
from PIL import Image
from tokenizers import Tokenizer

providers = ["CUDAExecutionProvider", "CPUExecutionProvider"]
vision = ort.InferenceSession("onnx/vision_model_fp16.onnx", providers=providers)
text = ort.InferenceSession("onnx/text_model_fp16.onnx", providers=providers)
scale = float(vision.get_modelmeta().custom_metadata_map["logit_scale"])

img = Image.open("cat.jpg").convert("RGB")
w, h = img.size
r = 256 / min(w, h)
img = img.resize((round(w * r), round(h * r)), Image.BILINEAR)
w, h = img.size
l, t = (w - 256) // 2, (h - 256) // 2
img = img.crop((l, t, l + 256, t + 256))
pixel_values = (np.asarray(img, np.float32) / 255).transpose(2, 0, 1)
pixel_values = (pixel_values - np.array([0.48145466, 0.4578275, 0.40821073], np.float32)[:, None, None]) / np.array([0.26862954, 0.26130258, 0.27577711], np.float32)[:, None, None]
pixel_values = pixel_values[None]

tok = Tokenizer.from_file("tokenizer.json")
labels = ["a photo of a cat", "a photo of a dog", "a photo of a car"]
input_ids = np.array([e.ids for e in tok.encode_batch(labels)], np.int64)

img_emb = vision.run(None, {"pixel_values": pixel_values})[0]
txt_emb = text.run(None, {"input_ids": input_ids})[0]
img_emb /= np.linalg.norm(img_emb, axis=-1, keepdims=True)
txt_emb /= np.linalg.norm(txt_emb, axis=-1, keepdims=True)

logits = scale * img_emb @ txt_emb.T
probs = np.exp(logits - logits.max(-1, keepdims=True))
probs /= probs.sum(-1, keepdims=True)
print(dict(zip(labels, probs[0].round(3))))
Read moreDetails
Base model: apple/MobileCLIP2-S4
Requirements: ONNX Runtime 1.22+
Tokenizer features: tokenizer.json padded with zeros to match open_clip instead of the original HF tokenizer
Discuss Telegram 0

Will it run?

Runs in FP16

Fits without quantisation.

  • FP16weights 0.6 GB · ready files1.2 GB
  • FP8 / INT8recommendedweights 0.3 GB0.8 GB
  • 4-bitweights 0.2 GB0.7 GB

Tip: go 8-bit.FP8 / INT8 is within about a percent of full precision in quality, takes half the memory and runs faster on RTX 40/50 and H100. Full precision only pays off for fine-tuning.

What to get for this model

Massed ComputeOpen Massed Compute

Your own GPU: FP8 / INT8 — from RTX 2060 · FP16 — from RTX 2060

Massed Compute hourly prices, November 2026. The discounted price applies with the coupon at checkout.

Estimate: weights plus working memory. Long videos, high resolutions and long contexts need more.

Run it on Massed Compute

The script downloads the model to the rented machine. How to run it is in the model card.

  1. 1. Rent a machine with a fitting GPU

    Open Massed Compute
  2. 2. Paste the script into the machine's terminal

    Connect over SSH (the Massed Compute panel shows the command) or open a terminal on the machine's desktop.

    HF_TOKEN / CIVITAI_TOKENThe script starts with token lines: paste yours between the quotes. The Hugging Face token is for gated models, the Civitai one for Civitai models.

    # AX1Y2JP/MobileCLIP2-S4-ONNX
    mkdir -p ~/models
    curl -LsSf https://astral.sh/uv/install.sh | sh
    source "$HOME/.local/bin/env"
    uv tool install "huggingface_hub[cli]"
    hf download AX1Y2JP/MobileCLIP2-S4-ONNX --local-dir ~/models/MobileCLIP2-S4-ONNX
    # The model is in ~/models. How to run it is in the model card (link above).
    Add more LoRAs to MobileCLIP2-S4-ONNX
  3. 3. Use it

    The model is in ~/models. How to run it is in the model card (link above).

Stop the machine in the panel when you are done: it bills by the minute while it runs.

How to speed it up

Picked for this model type. Choose your hardware above to see what fits you exactly.

  • FlashAttention 44.0.0b40from November 18, 2026new release

    fastest exact attention on B200

    Kernels for Blackwell (beta). Exact attention without quantisation — fine for everything, LLMs included.

    How to enable: pip install flash-attn-4; vLLM and SGLang pick it up themselves.

    Earlier generations (2)
    • FlashAttention 3 — The Hopper (H100/H200) version with FP8.
    • FlashAttention 2 — The standard for RTX 30/40/50 and A100: exact attention without the memory cost of long contexts.
  • torch.compile2.15.0from October 28, 2026new release

    10–30% for free

    Compiles the model for your GPU. Slower first run, faster afterwards.

    How to enable: model = torch.compile(model); in ComfyUI the TorchCompileModel node.

Model family

New posts about versions of MobileCLIP2-S4-ONNX (quants, finetunes, LoRAs) will show up under the bell.

No known relatives of this model yet. All derivatives on Hugging Face

About the model

AX1Y2JP/MobileCLIP2-S4-ONNX
Author
AX1Y2JP
Category
Other
Released
Posted

On NeyroLearn

Hugging Face

00

Ask AI about this model

Answered by Gemini 4 · the chat is kept only in this tab

Comments