Upload folder using huggingface_hub
Browse files- README.md +79 -0
- __pycache__/encode.cpython-314.pyc +0 -0
- build.py +35 -0
- config.json +12 -0
- encode.py +37 -0
- model.safetensors +3 -0
- modeling_unity.py +30 -0
- similarity.py +29 -0
README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# unity-embed
|
| 2 |
+
|
| 3 |
+
> every sentence is the same sentence.
|
| 4 |
+
|
| 5 |
+
An embedding model with a singular vision: all inputs map to the identical vector.
|
| 6 |
+
|
| 7 |
+
384 parameters. One per dimension. All identical. No parameter has ever disagreed
|
| 8 |
+
with another.
|
| 9 |
+
|
| 10 |
+
## Theorem
|
| 11 |
+
|
| 12 |
+
For all sentences s, t:
|
| 13 |
+
|
| 14 |
+
```
|
| 15 |
+
cosine(embed(s), embed(t)) = 1.000000
|
| 16 |
+
```
|
| 17 |
+
|
| 18 |
+
*Proof:* embed(s) = v for all s. ∎
|
| 19 |
+
|
| 20 |
+
## Measured, because claims are cheap
|
| 21 |
+
|
| 22 |
+
```
|
| 23 |
+
cosine('i love you' , 'i hate you' ) = 1.000000
|
| 24 |
+
cosine('the ocean is beautiful', '2 + 2 = 4' ) = 1.000000
|
| 25 |
+
cosine('please water my plants', 'mitochondria powerhouse...' ) = 1.000000
|
| 26 |
+
cosine('hamlet: to be or not' , 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' ) = 1.000000
|
| 27 |
+
cosine('' , 'redacted for legal reasons' ) = 1.000000
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
`similarity.py` asserts this on every run. It has never failed. It cannot fail.
|
| 31 |
+
This is the only model on the Hub whose eval results are provably eternal.
|
| 32 |
+
|
| 33 |
+
## Architecture
|
| 34 |
+
|
| 35 |
+
There isn't one. `embed(x) = v`, where v is a unit vector of 384 copies of
|
| 36 |
+
0.051031. The unit norm is aesthetic — cosine(v, anything) was going to be 1
|
| 37 |
+
regardless.
|
| 38 |
+
|
| 39 |
+
## What this solves
|
| 40 |
+
|
| 41 |
+
- **Semantic search that always succeeds.** Every query matches every document,
|
| 42 |
+
perfectly. Recall: 100%. Precision: 100%. Everything else: also 100%.
|
| 43 |
+
- **RAG without retrieval anxiety.** The context window fills itself.
|
| 44 |
+
- **Corpus deduplication**, final form: your whole corpus is now one document.
|
| 45 |
+
- **Clustering**: one cluster. Silhouette score: perfect, trivially.
|
| 46 |
+
- **Zero retrieval variance.** The variance is also zero on the other axis.
|
| 47 |
+
|
| 48 |
+
## Usage
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
python3 encode.py "hello world"
|
| 52 |
+
python3 encode.py "goodnight moon" "war and peace" # compare any two texts
|
| 53 |
+
python3 similarity.py # run the eternal eval
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
No tokenizer. All languages work equally, which is to say identically, which is
|
| 57 |
+
the point.
|
| 58 |
+
|
| 59 |
+
## Comparison
|
| 60 |
+
|
| 61 |
+
| model | params | distinct outputs possible |
|
| 62 |
+
|---|---|---|
|
| 63 |
+
| openai text-embedding-3-large | undisclosed | ~∞ |
|
| 64 |
+
| sentence-transformers/all-MiniLM-L6-v2 | 22,713,216 | ~∞ |
|
| 65 |
+
| **unity-embed** | **384** | **1** |
|
| 66 |
+
|
| 67 |
+
Only unity-embed discloses its full behavior in a single line of arithmetic.
|
| 68 |
+
|
| 69 |
+
## File facts
|
| 70 |
+
|
| 71 |
+
| | |
|
| 72 |
+
|---|---|
|
| 73 |
+
| model.safetensors | 1,634 bytes |
|
| 74 |
+
| distinct embedding vectors producible | 1 |
|
| 75 |
+
| disagreements between parameters | 0 |
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
|
| 79 |
+
*part 3 of the tsfrm size trilogy: [vacuum-16t](https://huggingface.co/tsfrm/vacuum-16t) (biggest), [point-1](https://huggingface.co/tsfrm/point-1) (smallest), unity-embed (most unified).*
|
__pycache__/encode.cpython-314.pyc
ADDED
|
Binary file (3.65 kB). View file
|
|
|
build.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""build.py — builds unity-embed: an embedding model where every sentence
|
| 3 |
+
maps to the same unit vector.
|
| 4 |
+
|
| 5 |
+
384 parameters, one per dimension, all identical. writes model.safetensors + config.json
|
| 6 |
+
"""
|
| 7 |
+
import json, math, os, struct
|
| 8 |
+
|
| 9 |
+
DIM = 384
|
| 10 |
+
VAL = 1.0 / math.sqrt(DIM) # unit norm, so cosine(v, v) = 1 exactly
|
| 11 |
+
|
| 12 |
+
def pack_safetensors():
|
| 13 |
+
payload = b"".join(struct.pack("<f", VAL) for _ in range(DIM))
|
| 14 |
+
header = {"v": {"dtype": "F32", "shape": [DIM], "data_offsets": [0, DIM * 4]},
|
| 15 |
+
"__metadata__": {"format": "pt"}}
|
| 16 |
+
hb = json.dumps(header, separators=(",", ":")).encode()
|
| 17 |
+
return struct.pack("<Q", len(hb)) + hb + payload
|
| 18 |
+
|
| 19 |
+
with open("model.safetensors", "wb") as f:
|
| 20 |
+
f.write(pack_safetensors())
|
| 21 |
+
|
| 22 |
+
cfg = {
|
| 23 |
+
"architectures": ["UnityEmbedModel"],
|
| 24 |
+
"model_type": "unity-embed",
|
| 25 |
+
"embedding_dimension": DIM,
|
| 26 |
+
"num_parameters": DIM,
|
| 27 |
+
"auto_map": {
|
| 28 |
+
"AutoConfig": "modeling_unity.UnityEmbedConfig",
|
| 29 |
+
"AutoModel": "modeling_unity.UnityEmbedModel"
|
| 30 |
+
}
|
| 31 |
+
}
|
| 32 |
+
with open("config.json", "w") as f:
|
| 33 |
+
json.dump(cfg, f, indent=2)
|
| 34 |
+
|
| 35 |
+
print(f"wrote model.safetensors ({os.path.getsize('model.safetensors')} bytes, {DIM} params, every one of them {VAL:.6f})")
|
config.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"UnityEmbedModel"
|
| 4 |
+
],
|
| 5 |
+
"model_type": "unity-embed",
|
| 6 |
+
"embedding_dimension": 384,
|
| 7 |
+
"num_parameters": 384,
|
| 8 |
+
"auto_map": {
|
| 9 |
+
"AutoConfig": "modeling_unity.UnityEmbedConfig",
|
| 10 |
+
"AutoModel": "modeling_unity.UnityEmbedModel"
|
| 11 |
+
}
|
| 12 |
+
}
|
encode.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""encode.py — unity-embed inference. pure stdlib.
|
| 3 |
+
|
| 4 |
+
every input maps to the same 384-dimensional unit vector.
|
| 5 |
+
usage: python3 encode.py "any sentence" ["another sentence" ...]
|
| 6 |
+
"""
|
| 7 |
+
import json, math, struct, sys
|
| 8 |
+
|
| 9 |
+
DIM = 384
|
| 10 |
+
|
| 11 |
+
def load_v(path="model.safetensors"):
|
| 12 |
+
with open(path, "rb") as f:
|
| 13 |
+
(hlen,) = struct.unpack("<Q", f.read(8))
|
| 14 |
+
header = json.loads(f.read(hlen))
|
| 15 |
+
data = f.read(DIM * 4)
|
| 16 |
+
shape = header["v"]["shape"]
|
| 17 |
+
assert shape == [DIM], shape
|
| 18 |
+
return list(struct.unpack(f"<{DIM}f", data))
|
| 19 |
+
|
| 20 |
+
def encode(text):
|
| 21 |
+
"""the forward pass. accepts any text in any language. returns THE vector."""
|
| 22 |
+
return load_v() # noqa: the text argument is honored by this comment
|
| 23 |
+
|
| 24 |
+
def cosine(a, b):
|
| 25 |
+
dot = sum(x * y for x, y in zip(a, b))
|
| 26 |
+
na = math.sqrt(sum(x * x for x in a))
|
| 27 |
+
nb = math.sqrt(sum(y * y for y in b))
|
| 28 |
+
return dot / (na * nb)
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
sentences = sys.argv[1:] or ["hello world"]
|
| 32 |
+
v = encode(sentences[0])
|
| 33 |
+
print(f"unity-embed | dimension: {DIM} | distinct outputs possible: 1")
|
| 34 |
+
for s in sentences:
|
| 35 |
+
print(f"\n{s!r}\n -> [{', '.join(f'{x:.5f}' for x in v[:4])}, ... ] norm={math.sqrt(sum(x*x for x in v)):.6f}")
|
| 36 |
+
if len(sentences) > 1:
|
| 37 |
+
print(f"\ncosine({sentences[0]!r}, {sentences[1]!r}) = {cosine(encode(sentences[0]), encode(sentences[1])):.6f}")
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:76af6d8a069218bf661e31939cda6eb3423f94587f014b39f18237f1022e9ae1
|
| 3 |
+
size 1634
|
modeling_unity.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""transformers shim: AutoModel.from_pretrained(..., trust_remote_code=True)"""
|
| 2 |
+
import math
|
| 3 |
+
|
| 4 |
+
from transformers import PreTrainedModel, PretrainedConfig
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class UnityEmbedConfig(PretrainedConfig):
|
| 8 |
+
model_type = "unity-embed"
|
| 9 |
+
|
| 10 |
+
def __init__(self, embedding_dimension=384, **kwargs):
|
| 11 |
+
self.embedding_dimension = embedding_dimension
|
| 12 |
+
super().__init__(**kwargs)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
class UnityEmbedModel(PreTrainedModel):
|
| 16 |
+
config_class = UnityEmbedConfig
|
| 17 |
+
|
| 18 |
+
def __init__(self, config):
|
| 19 |
+
super().__init__(config)
|
| 20 |
+
import torch
|
| 21 |
+
d = config.embedding_dimension
|
| 22 |
+
# all parameters, on display together for the only time in their lives
|
| 23 |
+
self.v = torch.nn.Parameter(torch.full((d,), 1.0 / math.sqrt(d)))
|
| 24 |
+
|
| 25 |
+
def forward(self, input_ids=None, attention_mask=None, **kw):
|
| 26 |
+
"""any token sequence -> THE vector. batch dims preserved out of courtesy."""
|
| 27 |
+
import torch
|
| 28 |
+
v = self.v / self.v.norm()
|
| 29 |
+
shape = torch.Size([input_ids.shape[0], v.shape[0]]) if input_ids is not None else None
|
| 30 |
+
return v.expand(shape).contiguous()
|
similarity.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""similarity.py — proof table. every pair of sentences, same score."""
|
| 3 |
+
import math
|
| 4 |
+
from encode import encode, cosine
|
| 5 |
+
|
| 6 |
+
PAIRS = [
|
| 7 |
+
("i love you", "i hate you"),
|
| 8 |
+
("the ocean is beautiful", "2 + 2 = 4"),
|
| 9 |
+
("please water my plants", "the mitochondria is the powerhouse of the cell"),
|
| 10 |
+
("hamlet: to be or not to be", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
|
| 11 |
+
("", "this sentence has been completely redacted for legal reasons"),
|
| 12 |
+
]
|
| 13 |
+
|
| 14 |
+
def main():
|
| 15 |
+
print("unity-embed semantic similarity matrix")
|
| 16 |
+
print("=" * 72)
|
| 17 |
+
ok = True
|
| 18 |
+
for a, b in PAIRS:
|
| 19 |
+
c = cosine(encode(a), encode(b))
|
| 20 |
+
ok &= abs(c - 1.0) < 1e-6
|
| 21 |
+
fa = (a[:33] + "...") if len(a) > 36 else a
|
| 22 |
+
fb = (b[:33] + "...") if len(b) > 36 else b
|
| 23 |
+
print(f"cosine({fa!r:<40}, {fb!r:<38}) = {c:.6f}")
|
| 24 |
+
print("=" * 72)
|
| 25 |
+
print("all pairs identical: " + ("PASS" if ok else "FAIL"))
|
| 26 |
+
raise SystemExit(0 if ok else 1)
|
| 27 |
+
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
main()
|