Download graph-allids.py from ppbrown/tokenspace: direct link, hf CLI and curl.
- Browser
- Download file 1.5 kB
-
https://huggingface.co/datasets/ppbrown/tokenspace/resolve/main/graph-allids.py
- Command line
-
hf download hf://datasets/ppbrown/tokenspace/graph-allids.py
-
curl -L -o graph-allids.py https://huggingface.co/datasets/ppbrown/tokenspace/resolve/main/graph-allids.py
1.5 kB
| #!/bin/env python | |
| """ 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="embeddings.allids.safetensors" | |
| 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="variance" | |
| type="mean" | |
| print(f"calculating {type}...") | |
| #emb1 = torch.var(embs,dim=0) | |
| 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") | |
| # Add labels, title, and legend | |
| #ax.set_xlabel('Index') | |
| ax.set_ylabel('Values') | |
| ax.set_title(f'Graph of {embed_file}') | |
| ax.legend() | |
| # Display the graph | |
| print("Pulling up the graph") | |
| plt.show() | |