Upload generate-distances.py
Browse files- generate-distances.py +37 -0
generate-distances.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/python3
|
| 2 |
+
|
| 3 |
+
""" Work in progress
|
| 4 |
+
Plan:
|
| 5 |
+
Read in fullword.json for list of words and token
|
| 6 |
+
Read in pre-calculated "proper" embedding for each token from safetensor file
|
| 7 |
+
Generate a tensor array of distance for each token, to every other token/embedding
|
| 8 |
+
Save it out
|
| 9 |
+
"""
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
import sys
|
| 13 |
+
import json
|
| 14 |
+
import torch
|
| 15 |
+
from safetensors import safe_open
|
| 16 |
+
|
| 17 |
+
embed_file="embeddings.safetensors"
|
| 18 |
+
|
| 19 |
+
device=torch.device("cuda")
|
| 20 |
+
|
| 21 |
+
print("read in words from json now",file=sys.stderr)
|
| 22 |
+
with open("fullword.json","r") as f:
|
| 23 |
+
tokendict = json.load(f)
|
| 24 |
+
|
| 25 |
+
print("read in embeddingsnow",file=sys.stderr)
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
model = safe_open(embed_file,framework="pt",device="cuda")
|
| 29 |
+
embs=model.get_tensor("embeddings")
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
print("Shape of result = ",embs.shape)
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
print("calculate distances now")
|
| 36 |
+
|
| 37 |
+
|