File size: 2,604 Bytes
7bc3de2
 
 
 
 
 
 
 
 
 
 
 
 
4924822
 
 
 
 
 
 
 
 
 
 
 
7bc3de2
 
 
 
 
 
 
 
 
4924822
7bc3de2
 
 
 
 
4924822
 
7bc3de2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/bin/env python

""" 
Plan:
   Read in "dictionary" for list of words
   Read in pre-calculated "proper" embedding for each word from safetensor file
   Prompt user for a word from the list
   Generate a tensor array of distance to all the other known words
   Print out the 20 closest ones
"""


import sys

mtype='ViT-H-14'
mname='laion2B-s32B-b79K'

print(f"Configured for {mtype}, {mname}")

if len(sys.argv) < 3:
    print("Error: need embeddings file and dictionary name")
    sys.exit(1)



import torch
import open_clip

from safetensors import safe_open

#from transformers import CLIPProcessor,CLIPModel

device=torch.device("cuda")

print("Loading model")

cmodel, _, preprocess = open_clip.create_model_and_transforms(mtype,
        pretrained=mname)
tokenizer = open_clip.get_tokenizer(mtype)

print("Moving model to cuda")
cmodel.to(device)


#embed_file="embeddings.safetensors"
embed_file=sys.argv[1]
dictionary=sys.argv[2]


print(f"read in words from {dictionary} now",file=sys.stderr)
with open(dictionary,"r") as f:
    tokendict = f.readlines()
    wordlist = [token.strip() for token in tokendict]  # Remove trailing newlines
print(len(wordlist),"lines read")

print(f"read in {embed_file} now",file=sys.stderr)
emodel = safe_open(embed_file,framework="pt",device="cuda")
embs=emodel.get_tensor("embeddings")
embs.to(device)
print("Shape of loaded embeds =",embs.shape)

def standard_embed_calc(text):
    with torch.no_grad():
        ttext = tokenizer(text)
        text_features = cmodel.encode_text(ttext)
    embedding = text_features[0]
    #print("shape of text is",ttext.shape)
    return embedding


def print_distances(targetemb):
    targetdistances = torch.cdist( targetemb.unsqueeze(0), embs, p=2)

    print("shape of distances...",targetdistances.shape)

    smallest_distances, smallest_indices = torch.topk(targetdistances[0], 20, largest=False)

    smallest_distances=smallest_distances.tolist()
    smallest_indices=smallest_indices.tolist()
    for d,i in zip(smallest_distances,smallest_indices):
        print(wordlist[i],"(",d,")")



# Find 10 closest tokens to targetword.
# Will include the word itself
def find_closest(targetword):
    try:
        targetindex=wordlist.index(targetword)
        targetemb=embs[targetindex]
        print_distances(targetemb)
        return
    except ValueError:
        print(targetword,"not found in cache")


    print("Now doing with full calc embed")
    targetemb=standard_embed_calc(targetword)
    print_distances(targetemb)


while True:
    input_text=input("Input a word now:")
    find_closest(input_text)