faster-clip 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- faster_clip-0.1.0/LICENSE +21 -0
- faster_clip-0.1.0/PKG-INFO +103 -0
- faster_clip-0.1.0/README.md +79 -0
- faster_clip-0.1.0/faster_clip/__init__.py +17 -0
- faster_clip-0.1.0/faster_clip/_nn.py +51 -0
- faster_clip-0.1.0/faster_clip/assets/splade_tokenizer.json +30684 -0
- faster_clip-0.1.0/faster_clip/assets/splade_weight.npy +0 -0
- faster_clip-0.1.0/faster_clip/clip.py +140 -0
- faster_clip-0.1.0/faster_clip/fillmask.py +101 -0
- faster_clip-0.1.0/faster_clip/sparse.py +48 -0
- faster_clip-0.1.0/faster_clip.egg-info/PKG-INFO +103 -0
- faster_clip-0.1.0/faster_clip.egg-info/SOURCES.txt +15 -0
- faster_clip-0.1.0/faster_clip.egg-info/dependency_links.txt +1 -0
- faster_clip-0.1.0/faster_clip.egg-info/requires.txt +5 -0
- faster_clip-0.1.0/faster_clip.egg-info/top_level.txt +1 -0
- faster_clip-0.1.0/pyproject.toml +37 -0
- faster_clip-0.1.0/setup.cfg +4 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 cnmoro
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: faster-clip
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Dependency-light pure-numpy CPU inference for CLIP embeddings, inference-free SPLADE, and BERT fill-mask — a drop-in replacement for the transformers / sentence-transformers models in a RAG stack (no PyTorch, no ONNX).
|
|
5
|
+
Author: cnmoro
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/cnmoro/faster-clip
|
|
8
|
+
Project-URL: Repository, https://github.com/cnmoro/faster-clip
|
|
9
|
+
Keywords: clip,splade,sparse-embeddings,fill-mask,embeddings,rag,numpy,no-torch
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: numpy
|
|
19
|
+
Requires-Dist: tokenizers
|
|
20
|
+
Requires-Dist: safetensors
|
|
21
|
+
Requires-Dist: huggingface_hub
|
|
22
|
+
Requires-Dist: pillow
|
|
23
|
+
Dynamic: license-file
|
|
24
|
+
|
|
25
|
+
# faster-clip
|
|
26
|
+
|
|
27
|
+
Dependency-light, **pure-numpy** CPU inference for the models a multilingual RAG
|
|
28
|
+
stack typically needs — with **no PyTorch, no transformers, no sentence-transformers,
|
|
29
|
+
no ONNX**. Just `numpy` + `tokenizers` (+ `safetensors` / `huggingface_hub` for
|
|
30
|
+
weights, `pillow` for images).
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install faster-clip
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
It provides three drop-in replacements, each **numerically matching** the original
|
|
37
|
+
HuggingFace pipeline:
|
|
38
|
+
|
|
39
|
+
| Class | Replaces | Output |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `ClipModel` | `SentenceTransformer('clip-ViT-B-32')` + `clip-ViT-B-32-multilingual-v1` | 512-d CLIP image / text embeddings (cosine ≈ 1.0) |
|
|
42
|
+
| `SparseEncoder` | `SparseEncoder('…inference-free-splade…')` | `{token_id: weight}` sparse dicts (**bit-identical**) |
|
|
43
|
+
| `FillMask` | `pipeline('fill-mask', 'bert-base-uncased' / 'bert-base-portuguese-cased')` | top-k `{token_str, sequence, score}` (**identical strings**) |
|
|
44
|
+
|
|
45
|
+
Model weights are downloaded from HuggingFace and cached on first use (the SPLADE
|
|
46
|
+
weights, ~120 KB, ship inside the wheel).
|
|
47
|
+
|
|
48
|
+
## CLIP embeddings
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from faster_clip import ClipModel
|
|
52
|
+
from PIL import Image
|
|
53
|
+
|
|
54
|
+
clip = ClipModel() # lazily downloads image / text weights on first use
|
|
55
|
+
|
|
56
|
+
text_emb = clip.encode_text(["um carro vermelho", "a red car"]) # (2, 512)
|
|
57
|
+
img_emb = clip.encode_image(Image.open("photo.jpg")) # (1, 512)
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Image and multilingual text land in the **same** 512-d CLIP space, so you can do
|
|
61
|
+
cross-modal search. (Normalize yourself if you need unit vectors.)
|
|
62
|
+
|
|
63
|
+
## SPLADE sparse encoding (no sparse tensor materialized)
|
|
64
|
+
|
|
65
|
+
The model is an *inference-free* SPLADE: encoding is a pure token→weight lookup,
|
|
66
|
+
so the result is just a dict of the active dimensions — no dense/sparse vector is
|
|
67
|
+
ever built.
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from faster_clip import SparseEncoder
|
|
71
|
+
|
|
72
|
+
sparse = SparseEncoder()
|
|
73
|
+
sparse.encode("banco de dados vetorial")
|
|
74
|
+
# -> {'15060': 1.09, '2951': 1.11, '2078': 1.04, ...} # {token_id: weight}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
`encode`, `encode_query`, and `encode_document` are equivalent (the model's query
|
|
78
|
+
route is the default) and accept a string or a list of strings.
|
|
79
|
+
|
|
80
|
+
## Fill-mask autocomplete
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
from faster_clip import FillMask
|
|
84
|
+
|
|
85
|
+
fm = FillMask()
|
|
86
|
+
fm.predict("the weather is very [MASK]", lang="en")
|
|
87
|
+
# -> [{'token_str': '.', 'sequence': 'the weather is very.', 'score': 0.30}, ...]
|
|
88
|
+
fm.predict("o brasil é um [MASK]", lang="pt")
|
|
89
|
+
# -> [{'token_str': 'lixo', 'sequence': 'o brasil é um lixo', ...}, ...]
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
`lang` is `"en"` (`bert-base-uncased`) or `"pt"` (`neuralmind/bert-base-portuguese-cased`).
|
|
93
|
+
|
|
94
|
+
## Model cache
|
|
95
|
+
|
|
96
|
+
Weights are cached by `huggingface_hub` under `~/.cache/huggingface`. To run
|
|
97
|
+
offline, pre-download the models once (or set `HF_HOME`). CLIP vision/text and the
|
|
98
|
+
BERT MLMs are fetched on first use of each capability.
|
|
99
|
+
|
|
100
|
+
## License
|
|
101
|
+
|
|
102
|
+
MIT. Model weights belong to their respective authors (OpenAI / Microsoft /
|
|
103
|
+
sentence-transformers / neuralmind), used under their original licenses.
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# faster-clip
|
|
2
|
+
|
|
3
|
+
Dependency-light, **pure-numpy** CPU inference for the models a multilingual RAG
|
|
4
|
+
stack typically needs — with **no PyTorch, no transformers, no sentence-transformers,
|
|
5
|
+
no ONNX**. Just `numpy` + `tokenizers` (+ `safetensors` / `huggingface_hub` for
|
|
6
|
+
weights, `pillow` for images).
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install faster-clip
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
It provides three drop-in replacements, each **numerically matching** the original
|
|
13
|
+
HuggingFace pipeline:
|
|
14
|
+
|
|
15
|
+
| Class | Replaces | Output |
|
|
16
|
+
|---|---|---|
|
|
17
|
+
| `ClipModel` | `SentenceTransformer('clip-ViT-B-32')` + `clip-ViT-B-32-multilingual-v1` | 512-d CLIP image / text embeddings (cosine ≈ 1.0) |
|
|
18
|
+
| `SparseEncoder` | `SparseEncoder('…inference-free-splade…')` | `{token_id: weight}` sparse dicts (**bit-identical**) |
|
|
19
|
+
| `FillMask` | `pipeline('fill-mask', 'bert-base-uncased' / 'bert-base-portuguese-cased')` | top-k `{token_str, sequence, score}` (**identical strings**) |
|
|
20
|
+
|
|
21
|
+
Model weights are downloaded from HuggingFace and cached on first use (the SPLADE
|
|
22
|
+
weights, ~120 KB, ship inside the wheel).
|
|
23
|
+
|
|
24
|
+
## CLIP embeddings
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from faster_clip import ClipModel
|
|
28
|
+
from PIL import Image
|
|
29
|
+
|
|
30
|
+
clip = ClipModel() # lazily downloads image / text weights on first use
|
|
31
|
+
|
|
32
|
+
text_emb = clip.encode_text(["um carro vermelho", "a red car"]) # (2, 512)
|
|
33
|
+
img_emb = clip.encode_image(Image.open("photo.jpg")) # (1, 512)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Image and multilingual text land in the **same** 512-d CLIP space, so you can do
|
|
37
|
+
cross-modal search. (Normalize yourself if you need unit vectors.)
|
|
38
|
+
|
|
39
|
+
## SPLADE sparse encoding (no sparse tensor materialized)
|
|
40
|
+
|
|
41
|
+
The model is an *inference-free* SPLADE: encoding is a pure token→weight lookup,
|
|
42
|
+
so the result is just a dict of the active dimensions — no dense/sparse vector is
|
|
43
|
+
ever built.
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from faster_clip import SparseEncoder
|
|
47
|
+
|
|
48
|
+
sparse = SparseEncoder()
|
|
49
|
+
sparse.encode("banco de dados vetorial")
|
|
50
|
+
# -> {'15060': 1.09, '2951': 1.11, '2078': 1.04, ...} # {token_id: weight}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`encode`, `encode_query`, and `encode_document` are equivalent (the model's query
|
|
54
|
+
route is the default) and accept a string or a list of strings.
|
|
55
|
+
|
|
56
|
+
## Fill-mask autocomplete
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from faster_clip import FillMask
|
|
60
|
+
|
|
61
|
+
fm = FillMask()
|
|
62
|
+
fm.predict("the weather is very [MASK]", lang="en")
|
|
63
|
+
# -> [{'token_str': '.', 'sequence': 'the weather is very.', 'score': 0.30}, ...]
|
|
64
|
+
fm.predict("o brasil é um [MASK]", lang="pt")
|
|
65
|
+
# -> [{'token_str': 'lixo', 'sequence': 'o brasil é um lixo', ...}, ...]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`lang` is `"en"` (`bert-base-uncased`) or `"pt"` (`neuralmind/bert-base-portuguese-cased`).
|
|
69
|
+
|
|
70
|
+
## Model cache
|
|
71
|
+
|
|
72
|
+
Weights are cached by `huggingface_hub` under `~/.cache/huggingface`. To run
|
|
73
|
+
offline, pre-download the models once (or set `HF_HOME`). CLIP vision/text and the
|
|
74
|
+
BERT MLMs are fetched on first use of each capability.
|
|
75
|
+
|
|
76
|
+
## License
|
|
77
|
+
|
|
78
|
+
MIT. Model weights belong to their respective authors (OpenAI / Microsoft /
|
|
79
|
+
sentence-transformers / neuralmind), used under their original licenses.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""faster-clip — dependency-light CPU replacements (pure numpy) for the
|
|
2
|
+
transformers / sentence-transformers models used in the RAG stack:
|
|
3
|
+
|
|
4
|
+
* :class:`ClipModel` — CLIP image + multilingual-text embeddings (512-d).
|
|
5
|
+
* :class:`SparseEncoder` — inference-free SPLADE sparse ``{token_id: weight}`` dicts.
|
|
6
|
+
* :class:`FillMask` — BERT masked-LM autocomplete (en / pt).
|
|
7
|
+
|
|
8
|
+
No PyTorch, no transformers, no ONNX — just numpy + tokenizers (+ safetensors,
|
|
9
|
+
huggingface_hub for weights, pillow for images).
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .clip import ClipModel
|
|
13
|
+
from .fillmask import FillMask
|
|
14
|
+
from .sparse import SparseEncoder
|
|
15
|
+
|
|
16
|
+
__all__ = ["ClipModel", "SparseEncoder", "FillMask"]
|
|
17
|
+
__version__ = "0.1.0"
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"""Minimal numpy neural-net primitives shared by the encoders."""
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def layer_norm(x, w, b, eps=1e-5):
|
|
7
|
+
mu = x.mean(-1, keepdims=True)
|
|
8
|
+
var = x.var(-1, keepdims=True) # population variance (ddof=0), matches torch
|
|
9
|
+
return (x - mu) / np.sqrt(var + eps) * w + b
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def quick_gelu(x):
|
|
13
|
+
return x * (1.0 / (1.0 + np.exp(-1.702 * x)))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _erf(x):
|
|
17
|
+
# Abramowitz & Stegun 7.1.26, |error| < 1.5e-7
|
|
18
|
+
s = np.sign(x)
|
|
19
|
+
x = np.abs(x)
|
|
20
|
+
t = 1.0 / (1.0 + 0.3275911 * x)
|
|
21
|
+
y = 1.0 - (((((1.061405429 * t - 1.453152027) * t) + 1.421413741) * t
|
|
22
|
+
- 0.284496736) * t + 0.254829592) * t * np.exp(-x * x)
|
|
23
|
+
return s * y
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def gelu(x):
|
|
27
|
+
return 0.5 * x * (1.0 + _erf(x / np.sqrt(2.0)))
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def softmax(x, axis=-1):
|
|
31
|
+
x = x - x.max(axis=axis, keepdims=True)
|
|
32
|
+
e = np.exp(x)
|
|
33
|
+
return e / e.sum(axis=axis, keepdims=True)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def multi_head_attention(x, wq, bq, wk, bk, wv, bv, wo, bo, n_heads):
|
|
37
|
+
"""Self-attention over a single (seq, dim) sequence. Weights are torch [out, in]."""
|
|
38
|
+
seq, dim = x.shape
|
|
39
|
+
hd = dim // n_heads
|
|
40
|
+
q = (x @ wq.T + bq).reshape(seq, n_heads, hd).transpose(1, 0, 2)
|
|
41
|
+
k = (x @ wk.T + bk).reshape(seq, n_heads, hd).transpose(1, 0, 2)
|
|
42
|
+
v = (x @ wv.T + bv).reshape(seq, n_heads, hd).transpose(1, 0, 2)
|
|
43
|
+
scores = q @ k.transpose(0, 2, 1) / np.sqrt(hd)
|
|
44
|
+
ctx = softmax(scores, -1) @ v # (h, seq, hd)
|
|
45
|
+
ctx = ctx.transpose(1, 0, 2).reshape(seq, dim)
|
|
46
|
+
return ctx @ wo.T + bo
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def linear(x, w, b=None):
|
|
50
|
+
y = x @ w.T
|
|
51
|
+
return y if b is None else y + b
|