Skip to main content

Fast CPU inference (Rust) for CLIP embeddings, inference-free SPLADE, and BERT fill-mask — drop-in replacement for the transformers / sentence-transformers models in a RAG stack. No PyTorch, no ONNX.

Project description

faster-clip

Fast, dependency-light CPU inference for the models a multilingual RAG stack needs — no PyTorch, no transformers, no sentence-transformers, no ONNX. Since 0.2 the compute is a hand-written Rust engine (a single native extension); tokenization uses the tokenizers crate.

pip install faster-clip

Three drop-in replacements, each numerically matching the original HuggingFace pipeline:

Class Replaces Output
ClipModel SentenceTransformer('clip-ViT-B-32') + …-multilingual-v1 512-d CLIP image / text embeddings (cosine 1.0)
SparseEncoder SparseEncoder('…inference-free-splade…') {token_id: weight} sparse dicts (bit-identical)
FillMask pipeline('fill-mask', 'bert-base-uncased' / 'bert-base-portuguese-cased') top-k {token_str, sequence, score} (identical strings)

Performance

CPU (AMD Ryzen 7 3700X, 8 cores), single item, vs the original torch pipeline:

torch / sentence-transformers faster-clip (Rust)
CLIP text embed ~10 ms ~13 ms (≈ parity)
CLIP image embed ~62 ms ~91 ms
fill-mask (BERT) ~60 ms ~25 ms (2.4× faster)
SPLADE sparse < 0.05 ms (pure lookup)

No torch/transformers install (multi-GB) — one native wheel.

CLIP embeddings

from faster_clip import ClipModel

clip = ClipModel()  # lazily downloads image / text weights on first use
text_emb = clip.encode_text(["um carro vermelho", "a red car"])   # (2, 512) list
img_emb  = clip.encode_image([open("photo.jpg", "rb").read()])    # (1, 512) list
  • encode_text(list_of_str) → list of 512-float vectors.
  • encode_image(list_of_image_bytes) → list of 512-float vectors. Pass the raw image bytes (PNG/JPEG/…); decoding + CLIP preprocessing happen in Rust.

Image and multilingual text share the same 512-d space. Normalize yourself for cosine search.

SPLADE sparse encoding (no sparse tensor materialised)

from faster_clip import SparseEncoder
sparse = SparseEncoder()
sparse.encode("banco de dados vetorial")     # -> {'15060': 1.09, '2951': 1.11, ...}

Inference-free: a token→weight lookup, so only the active dims are returned — no dense/sparse vector is ever built. encode, encode_query, encode_document are equivalent; each takes a str or list of str. The weights ship in the wheel.

Fill-mask autocomplete

from faster_clip import FillMask
fm = FillMask()
fm.predict("the weather is very [MASK]", lang="en")
fm.predict("o brasil é um [MASK]", lang="pt")
# -> [{'token_str': 'lixo', 'sequence': 'o brasil é um lixo', 'score': 0.18}, ...]

lang is "en" (bert-base-uncased) or "pt" (neuralmind/bert-base-portuguese-cased).

Model cache

CLIP and BERT weights are downloaded on first use and cached under ~/.cache/faster-clip (override with $FASTER_CLIP_CACHE). Inference releases the GIL, so one instance is safe to share across threads.

License

MIT. Model weights belong to their respective authors (OpenAI / sentence-transformers / neuralmind / Microsoft) under their original licenses.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faster_clip-0.2.0.tar.gz (395.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

faster_clip-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.17+ x86-64

File details

Details for the file faster_clip-0.2.0.tar.gz.

File metadata

  • Download URL: faster_clip-0.2.0.tar.gz
  • Upload date:
  • Size: 395.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for faster_clip-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a2d79c88631ab261fdc2f24538909d4dbee06c14aa798a07810d29f98233b3a7
MD5 8593b06ac18f139037dddbc19dca6fbd
BLAKE2b-256 e70e5bbe037e8b28f56713952bf119df333ee3c4447e5cf9144d22f66a10c635

See more details on using hashes here.

File details

Details for the file faster_clip-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for faster_clip-0.2.0-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c027fb458a8d44d7ca62590ca408ebd215fdf9ecbd7964078a25642a789430ef
MD5 bcddec22994ad4ba37de799daccc9738
BLAKE2b-256 5fb4d84f542c791a6f5e9e311a2dc76b7cd1e71575c9dd852f2497653b6b951f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page