tokenspace / compare-allids-embeds.py
ppbrown's picture
Upload 2 files
93e438f verified
raw
history blame
1.24 kB
#!/bin/env python
""" Work in progress
temp utility.
Load in two pre-calculated embeddings files.
(eg: *.allid.*)
Go through the full range and calculate distances between each.
Add up and display
This covers the full official range of tokenids,
0-49405
"""
import sys
import torch
from safetensors import safe_open
file1=sys.argv[1]
file2=sys.argv[2]
device=torch.device("cuda")
print(f"reading {file1} embeddings now",file=sys.stderr)
model = safe_open(file1,framework="pt",device="cuda")
embs1=model.get_tensor("embeddings")
embs1.to(device)
print("Shape of loaded embeds =",embs1.shape)
print(f"reading {file2} embeddings now",file=sys.stderr)
model = safe_open(file2,framework="pt",device="cuda")
embs2=model.get_tensor("embeddings")
embs2.to(device)
print("Shape of loaded embeds =",embs2.shape)
if torch.equal(embs1 , embs2):
print("HEY! Both files are identical!")
exit(0)
print(f"calculating distances...")
# This calculates a full cross matrix of ALL distances to ALL other points
# in other tensor
##targetdistances = torch.cdist( embs1,embs2, p=2)
targetdistances = torch.norm(embs2 - embs1, dim=1)
print(targetdistances.shape)
tl=targetdistances.tolist()
print(tl[:10])