ppbrown commited on
Commit
3523378
1 Parent(s): 84892d5

Upload graph-allids.py

Browse files

Util to visualize the "allembeddings" file

Files changed (1) hide show
  1. graph-allids.py +72 -0
graph-allids.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/env python
2
+
3
+ """ Work in progress
4
+ Plan:
5
+ Take a pre-calculated embeddings file.
6
+ calculate an average distance-from-origin across ALL IDs, and graph that.
7
+ Typically, you would use
8
+ "embeddings.allids.safetensors"
9
+ This covers the full official range of tokenids, 0-49405
10
+ But, you could use a partial file
11
+
12
+ """
13
+
14
+
15
+ #embed_file="embeddings.allids.safetensors"
16
+ embed_file="cliptextmodel.embeddings.allids.safetensors"
17
+
18
+
19
+ import sys
20
+ if len(sys.argv) !=2:
21
+ print("ERROR: Expect an embeddings.safetensors file as argument")
22
+ sys.exit(1)
23
+ embed_file=sys.argv[1]
24
+
25
+ import torch
26
+ from safetensors import safe_open
27
+
28
+ import PyQt5
29
+ import matplotlib
30
+ matplotlib.use('QT5Agg')
31
+
32
+ import matplotlib.pyplot as plt
33
+
34
+ device=torch.device("cuda")
35
+ print(f"reading {embed_file} embeddings now",file=sys.stderr)
36
+ model = safe_open(embed_file,framework="pt",device="cuda")
37
+ embs=model.get_tensor("embeddings")
38
+ embs.to(device)
39
+ print("Shape of loaded embeds =",embs.shape)
40
+
41
+ def embed_from_tokenid(num: int):
42
+ embed = embs[num]
43
+ return embed
44
+
45
+
46
+
47
+ fig, ax = plt.subplots()
48
+
49
+
50
+ #type="variance"
51
+ type="mean"
52
+ print(f"calculating {type}...")
53
+
54
+ #emb1 = torch.var(embs,dim=0)
55
+
56
+ emb1 = torch.mean(embs,dim=0)
57
+
58
+ print("shape of emb1:",emb1.shape)
59
+
60
+ graph1=emb1.tolist()
61
+ ax.plot(graph1, label=f"{type} of each all embedding")
62
+
63
+
64
+ # Add labels, title, and legend
65
+ #ax.set_xlabel('Index')
66
+ ax.set_ylabel('Values')
67
+ ax.set_title(f'Graph of {embed_file}')
68
+ ax.legend()
69
+
70
+ # Display the graph
71
+ print("Pulling up the graph")
72
+ plt.show()