|
|
|
|
|
""" Work in progress |
|
Plan: |
|
Take a pre-calculated embeddings file. |
|
calculate an average distance-from-origin across ALL IDs, and graph that. |
|
Typically, you would use |
|
"embeddings.allids.safetensors" |
|
This covers the full official range of tokenids, 0-49405 |
|
But, you could use a partial file |
|
|
|
""" |
|
|
|
|
|
|
|
embed_file="cliptextmodel.embeddings.allids.safetensors" |
|
|
|
|
|
import sys |
|
if len(sys.argv) !=2: |
|
print("ERROR: Expect an embeddings.safetensors file as argument") |
|
sys.exit(1) |
|
embed_file=sys.argv[1] |
|
|
|
import torch |
|
from safetensors import safe_open |
|
|
|
import PyQt5 |
|
import matplotlib |
|
matplotlib.use('QT5Agg') |
|
|
|
import matplotlib.pyplot as plt |
|
|
|
device=torch.device("cuda") |
|
print(f"reading {embed_file} embeddings now",file=sys.stderr) |
|
model = safe_open(embed_file,framework="pt",device="cuda") |
|
embs=model.get_tensor("embeddings") |
|
embs.to(device) |
|
print("Shape of loaded embeds =",embs.shape) |
|
|
|
def embed_from_tokenid(num: int): |
|
embed = embs[num] |
|
return embed |
|
|
|
|
|
|
|
fig, ax = plt.subplots() |
|
|
|
|
|
|
|
type="mean" |
|
print(f"calculating {type}...") |
|
|
|
|
|
|
|
emb1 = torch.mean(embs,dim=0) |
|
|
|
print("shape of emb1:",emb1.shape) |
|
|
|
graph1=emb1.tolist() |
|
ax.plot(graph1, label=f"{type} of each all embedding") |
|
|
|
|
|
|
|
|
|
ax.set_ylabel('Values') |
|
ax.set_title(f'Graph of {embed_file}') |
|
ax.legend() |
|
|
|
|
|
print("Pulling up the graph") |
|
plt.show() |
|
|