File size: 1,498 Bytes
c2d5538
2416d2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/env python

""" Work in progress
Plan:
   Similar to generate-embeddings.py
   However, instead of reading from a dictionary, just generate by pure
   numeric tokenID
   Save it out
"""


import sys
import json
import torch
from safetensors.torch import save_file
from transformers import CLIPProcessor,CLIPModel

clipsrc="openai/clip-vit-large-patch14"
processor=None
model=None

device=torch.device("cuda")


def init():
    global processor
    global model
    # Load the processor and model
    print("loading processor from "+clipsrc,file=sys.stderr)
    processor = CLIPProcessor.from_pretrained(clipsrc)
    print("done",file=sys.stderr)
    print("loading model from "+clipsrc,file=sys.stderr)
    model = CLIPModel.from_pretrained(clipsrc)
    print("done",file=sys.stderr)

    model = model.to(device)



def embed_from_inputs(inputs):
    with torch.no_grad():
        text_features = model.get_text_features(**inputs)
    embedding = text_features[0]

    return embedding


init()
inputs = processor(text="dummy", return_tensors="pt")
inputs.to(device)

all_embeddings = []

for id in range(49405):
    inputs.input_ids[0][1]=id

    emb=embed_from_inputs(inputs)
    emb=emb.unsqueeze(0) # stupid matrix magic to make the cat work
    all_embeddings.append(emb)
    if (id %100) ==0:
        print(id)

embs = torch.cat(all_embeddings,dim=0)
print("Shape of result = ",embs.shape)
print("Saving all the things...")
save_file({"embeddings": embs}, "embeddings.safetensors")