-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddings.py
291 lines (230 loc) · 11.5 KB
/
embeddings.py
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import psycopg2
#from documents import create_in_memory_csv
from vars import conn_params,get_strategy_by_name,make_strategy
from concurrent.futures import ThreadPoolExecutor
from questions import get_gpt_snippets_by_strategy
from transformers import AutoTokenizer,AutoModel
import torch
from tqdm import tqdm
def make_embeddings_table(conn, table_name, vector_size):
"""
Create a dynamic table for embeddings with a specified vector size, if it does not already exist.
:param conn: psycopg2 connection object to the database
:param table_name: Name of the table to be created
:param vector_size: Size of the vector for the 'embedding' column
"""
with conn.cursor() as cursor:
try:
# Try creating the table
create_table_sql = f"""
CREATE TABLE {table_name} (
embedding_id SERIAL PRIMARY KEY,
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
embedding vector({vector_size}),
text TEXT,
tokens INT[],
snippet_id INT NOT NULL,
strategy_id INT NOT NULL,
FOREIGN KEY (snippet_id) REFERENCES GPT_Snippets (snippet_id),
FOREIGN KEY (strategy_id) REFERENCES Strategies (strategy_id)
);
"""
cursor.execute(create_table_sql)
print(f"Table '{table_name}' created successfully.")
except psycopg2.errors.DuplicateTable:
# Handle duplicate table error
print(f"Table '{table_name}' already exists.")
conn.rollback()
def chunk_iterator(snippets,tokenizer,max_size,chunk_size):
ans=[]
for s in snippets:
tokens=tokenizer.encode(s['snippet_text'])
for i in range(0,len(tokens),max_size):
ans.append((s['snippet_id'],tokens[i:i+max_size]))
if(len(ans)==chunk_size):
yield ans
ans=[]
if(ans):
yield ans
# def tensor_iterator(snippets,tokenizer,chunk_size):
# ans=[]
# for s in snippets:
# output=tokenizer(s['snippet_text'],padding=True, truncation=True, return_tensors='pt')
# #print(output.keys())
# ans.append((s['snippet_id'],output['input_ids'],output['attention_mask']))
# if(len(ans)==chunk_size):
# yield ans
# ans=[]
# if(ans):
# yield ans
@torch.no_grad
def run_mean(tokens,mask,model):
mask=torch.IntTensor(mask).to(model.device)
tokens=torch.IntTensor(tokens).to(model.device)
#print(tokens.shape)
#print(mask.shape)
out=model(tokens,mask).last_hidden_state
mask=mask[:,:,None]
out*=mask
#print(out.sum(1).shape)
return (out.sum(1)/mask.sum(1)).cpu().tolist()
@torch.no_grad
def run_mean_checked(tokens,mask,model):
mask=torch.IntTensor(mask).to(model.device)
tokens=torch.IntTensor(tokens).to(model.device)
#print(tokens.shape)
#print(mask.shape)
out=model(input_ids=tokens,attention_mask=mask).last_hidden_state
#print(out)
mask=mask[:,:,None]
out*=mask
out=out.sum(1)
out=senetize(out)
#print(out.sum(1).shape)
return (out/mask.sum(1)).cpu().tolist()
def senetize(tensor):
# Replace +inf with the maximum float value
tensor = torch.where(tensor == torch.inf, torch.full_like(tensor, torch.finfo(tensor.dtype).max), tensor)
# Replace -inf with the minimum float value
tensor = torch.where(tensor == -torch.inf, torch.full_like(tensor, -torch.finfo(tensor.dtype).max), tensor)
tensor = torch.where(torch.isnan(tensor), torch.full_like(tensor, 0), tensor)
return tensor
# @torch.no_grad
# def run_mean_tensors(tokens,mask,model):
# print(tokens)
# print(mask)
# mask=mask.to(model.device)
# tokens=tokens.to(model.device)
# #print(tokens.shape)
# out=model(tokens,mask).last_hidden_state
# mask=mask[:,:,None]
# out*=mask
# #print(out.sum(1).shape)
# return (out.sum(1)/mask.sum(1)).cpu().tolist()
@torch.no_grad
def run_pooler(tokens,mask,model):
mask=torch.IntTensor(mask).to(model.device)
tokens=torch.IntTensor(tokens).to(model.device)
#print(tokens.shape)
return model(tokens,mask).pooler_output.cpu().tolist()
# def update_embeddings(conn,table_name,l):
# #print([len(x) for x in l[0][-2:]])
# with conn.cursor() as cursor:
# with ThreadPoolExecutor() as ex:
# #ex.
# list(map( lambda x: cursor.execute(f"""INSERT INTO {table_name}
# (snippet_id,strategy_id,tokens,embedding)
# VALUES (%s, %s, %s, %s)""",
# x),l))
def update_embeddings(conn,table_name,l):
#print([len(x) for x in l[0][-2:]])
with conn.cursor() as cursor:
cursor.executemany(f"""INSERT INTO {table_name}
(snippet_id,strategy_id,tokens,embedding)
VALUES (%s, %s, %s, %s)""",
l)
def update_embeddings_relaxed(conn, table_name, l):
try:
with conn.cursor() as cursor:
cursor.executemany(f"""INSERT INTO {table_name}
(snippet_id, strategy_id, tokens, embedding)
VALUES (%s, %s, %s, %s)""",
l)
conn.commit() # Commit changes only if all insertions are successful
except psycopg2.Error as e: # Catching PostgreSQL errors
print("PostgreSQL error:", e)
print("Input list that caused the error:", l)
# Optionally, you could roll back the transaction if you want to undo any changes made before the error
conn.rollback()
def make_naive_embedding(conn,read_id,write_id,table_name,tokenizer,model,chunk_size=500):
make_embeddings_table(conn,table_name,model.config.hidden_size)
max_size=model.config.max_position_embeddings
#print(max_size)
snippets=get_gpt_snippets_by_strategy(conn,read_id)
for c in chunk_iterator(tqdm(snippets),tokenizer,max_size,chunk_size):
mask=[[1]*len(x[1])+[0]*(max_size-len(x[1])) for x in c]
tokens=[x[1]+[0]*(max_size-len(x[1])) for x in c]
out=run_mean(tokens,mask,model)
update_embeddings(conn,table_name,[(x[0],write_id,x[1],o) for x,o in zip(c,out)])
def make_avg_embedding(conn,read_id,write_id,table_name,tokenizer,model,chunk_size=500):
make_embeddings_table(conn,table_name,model.config.hidden_size)
max_size=model.config.max_position_embeddings
#print(max_size)
snippets=get_gpt_snippets_by_strategy(conn,read_id)
for c in chunk_iterator(tqdm(snippets),tokenizer,max_size,chunk_size):
mask=[[1]*len(x[1])+[0]*(max_size-len(x[1])) for x in c]
tokens=[x[1]+[0]*(max_size-len(x[1])) for x in c]
out=run_mean_checked(tokens,mask,model)
update_embeddings(conn,table_name,[(x[0],write_id,x[1],o) for x,o in zip(c,out)])
def make_avg_embedding_relaxed(conn,read_id,write_id,table_name,tokenizer,model,chunk_size=500):
make_embeddings_table(conn,table_name,model.config.hidden_size)
max_size=model.config.max_position_embeddings
#print(max_size)
snippets=get_gpt_snippets_by_strategy(conn,read_id)
for c in chunk_iterator(tqdm(snippets),tokenizer,max_size,chunk_size):
mask=[[1]*len(x[1])+[0]*(max_size-len(x[1])) for x in c]
tokens=[x[1]+[0]*(max_size-len(x[1])) for x in c]
out=run_mean_checked(tokens,mask,model)
update_embeddings_relaxed(conn,table_name,[(x[0],write_id,x[1],o) for x,o in zip(c,out)])
def make_pooler_embedding(conn,read_id,write_id,table_name,tokenizer,model,chunk_size=500):
make_embeddings_table(conn,table_name,model.config.hidden_size)
max_size=model.config.max_position_embeddings
#print(max_size)
snippets=get_gpt_snippets_by_strategy(conn,read_id)
bar=tqdm(snippets)
print('got to iterator tqdn should exist')
for c in chunk_iterator(bar,tokenizer,max_size,chunk_size):
mask=[[1]*len(x[1])+[0]*(max_size-len(x[1])) for x in c]
tokens=[x[1]+[0]*(max_size-len(x[1])) for x in c]
out=run_pooler(tokens,mask,model)
update_embeddings(conn,table_name,[(x[0],write_id,x[1],o) for x,o in zip(c,out)])
# Example usage
if __name__ == "__main__":
#using avrage pooling because https://aclanthology.org/D19-1410.pdf
#model_name="bert-base-multilingual-cased"
#model_name="avichr/Legal-heBERT"
#model_name="avichr/heBERT"
#model_name="bert-base-uncased"
#model_name="models/bert-base-uncased_L2_v0"
#model_name="sentence-transformers/all-MiniLM-L6-v2"#(sbert)
#model_name="imvladikon/sentence-transformers-alephbert"
#model_name="thenlper/gte-base"#"aws-neuron/bge-base-en-v1-5-seqlen-384-bs-1"
#model_name="BAAI/bge-large-en-v1.5"
#model_name="llmrails/ember-v1"
#model_name="nomic-ai/nomic-embed-text-v1" #breaks the huggingface standard on argument order...
#model_name="yam-peleg/Hebrew-Gemma-11B" #too slow gona need to run in a place with gpu (with my local machine db talking to it)
#model_name="google/gemma-7b"
model_name="my_model"
model_path="/media/user/8a594cab-20d9-43ef-8d0e-b60b5cf43462/hebrew_search_stuff/results/checkpoint-2040000"
tokenizer_path="avichr/heBERT"
tokenizer=AutoTokenizer.from_pretrained(tokenizer_path)
model=AutoModel.from_pretrained(model_path)
#tokenizer=AutoTokenizer.from_pretrained(model_name)
#model=AutoModel.from_pretrained(model_name,load_in_4bit=True,bnb_4bit_compute_dtype=torch.float16)
print(model.config.max_position_embeddings)
#model.to('cuda')
#embedding_table_name=f"{model_name.replace('/','_').replace('-','_').replace('.','_')}_avrage_pool"
strat_name="naive"
#strat_name="test_based"
table_extra="squad_ContextFromQuestion_v1_hebrew"#"squad_ContextFromQuestion_v2_hebrew"#"squad_ContextFromQuestion_v2_"#"squad_ContextFromQuestion_"#"wiki_"
#BREAKING CHANGE
embedding_table_name=f"{table_extra}{model_name.replace('/','_').replace('-','_').replace('.','_')}_avrage_pool"
#embedding_table_name=f"{table_extra}{model_name.replace('/','_').replace('-','_').replace('.','_')}_pooler"
#print(model(**tokenizer("שלום",return_tensors="pt")).last_hidden_state.shape)
with psycopg2.connect(**conn_params) as conn:
#read_id=get_strategy_by_name(conn,"deafualt choped 1_000 10_000")['strategy_id']
#read_id=get_strategy_by_name(conn,"10wikipedia choped 100_000")['strategy_id']##
read_id=get_strategy_by_name(conn,"hebrew squad (question->context)")['strategy_id']
#read_id=get_strategy_by_name(conn,"ensglish squad (question->context)")['strategy_id']
#read_id=get_strategy_by_name(conn,"ensglish squad (question->context) v2")['strategy_id']
#read_id=get_strategy_by_name(conn,"hebrew squad (question->context) v2")['strategy_id']
#print(read_id)
write_id=get_strategy_by_name(conn,f"{model_name}:{strat_name}")#['strategy_id']
if(write_id==None):
write_id=make_strategy(conn,f"{model_name}:{strat_name}")
else:
write_id=write_id['strategy_id']
#make_naive_embedding(conn,read_id,write_id,embedding_table_name,tokenizer,model,chunk_size=64)#,chunk_size=32)#,chunk_size=1)#,chunk_size=32)
#make_pooler_embedding(conn,read_id,write_id,embedding_table_name,tokenizer,model)
#make_avg_embedding(conn,read_id,write_id,embedding_table_name,tokenizer,model,chunk_size=2)
make_avg_embedding_relaxed(conn,read_id,write_id,embedding_table_name,tokenizer,model,chunk_size=32)