File size: 12,560 Bytes
036acda |
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 |
from torch.utils.data import DataLoader
from sentence_transformers import losses, util, models
from sentence_transformers import SentencesDataset, LoggingHandler, SentenceTransformer, evaluation
from sentence_transformers.readers import InputExample
import logging
from datetime import datetime
import os
from shutil import copyfile
import sys
import math
import gzip
import random
import tqdm
from transformers import AutoTokenizer, AutoModel, BertModel
import transformers
import torch
from SPARTA import SPARTA
import json
import numpy as np
from torch.cuda.amp import autocast
import os
from shutil import copyfile
import datetime
from collections import defaultdict
from scipy.sparse import csc_matrix, csr_matrix
random.seed(42)
scaler = torch.cuda.amp.GradScaler()
#### Just some code to print debug information to stdout
logging.basicConfig(format='%(asctime)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging.INFO,
handlers=[LoggingHandler()])
#### /print debug information to stdout
# Fill GPU
fill_gpu = torch.eye(85000, dtype=torch.float, device='cuda')
del fill_gpu
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model_name = sys.argv[1]
model = SPARTA(model_name, device)
model_save_path = "output/msmarco-{}-{}".format(model_name.rstrip("/").split("/")[-1], datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
model.tokenizer.save_pretrained(model_save_path)
##Distil setting
if 'distil' in model_name:
batch_size, num_negatives = 4, 35
else:
batch_size, num_negatives = 3, 20
logging.info(f"batch_size: {batch_size}")
logging.info(f"num_neg: {num_negatives}")
# Write self to path
os.makedirs(model_save_path, exist_ok=True)
train_script_path = os.path.join(model_save_path, 'train_script.py')
copyfile(__file__, train_script_path)
with open(train_script_path, 'a') as fOut:
fOut.write("\n\n# Script was called via:\n#python " + " ".join(sys.argv))
########################
corpus = {}
train_queries = {}
#### Read dev file
logging.info("Create dev dataset")
dev_corpus_max_size = 100*1000
dev_queries_file = '../data/queries.dev.small.tsv'
needed_pids = set()
needed_qids = set()
dev_qids = set()
dev_queries = {}
dev_corpus = {}
dev_rel_docs = {}
with open(dev_queries_file) as fIn:
for line in fIn:
qid, query = line.strip().split("\t")
dev_qids.add(qid)
with open('../data/qrels.dev.tsv') as fIn:
for line in fIn:
qid, _, pid, _ = line.strip().split('\t')
if qid not in dev_qids:
continue
if qid not in dev_rel_docs:
dev_rel_docs[qid] = set()
dev_rel_docs[qid].add(pid)
needed_pids.add(pid)
needed_qids.add(qid)
with open(dev_queries_file) as fIn:
for line in fIn:
qid, query = line.strip().split("\t")
if qid in needed_qids:
dev_queries[qid] = query
with gzip.open('../data/collection-rnd.tsv.gz', 'rt') as fIn:
for line in fIn:
pid, passage = line.strip().split("\t")
if pid in needed_pids or dev_corpus_max_size <= 0 or len(dev_corpus) <= dev_corpus_max_size:
dev_corpus[pid] = passage
dev_corpus_pids = list(dev_corpus.keys())
dev_corpus = [dev_corpus[pid] for pid in dev_corpus_pids]
########### Eval functions
def compute_passage_emb(passages):
sparse_embeddings = []
bert_input_emb = model.bert_model.embeddings.word_embeddings(torch.tensor(list(range(0, len(model.tokenizer))), device=device))
sparse_vec_size = 2000
# Set Special tokens [CLS] [MASK] etc. to zero
for special_id in model.tokenizer.all_special_ids:
bert_input_emb[special_id] = 0 * bert_input_emb[special_id]
with torch.no_grad():
tokens = model.tokenizer(passages, padding=True, truncation=True, return_tensors='pt', max_length=500).to(device)
passage_embeddings = model.bert_model(**tokens).last_hidden_state
for passage_emb in passage_embeddings:
scores = torch.matmul(bert_input_emb, passage_emb.transpose(0, 1))
max_scores = torch.max(scores, dim=-1).values
relu_scores = torch.relu(max_scores) #Eq. 5
final_scores = torch.log(relu_scores + 1) # Eq. 6, final score
top_results = torch.topk(final_scores, k=sparse_vec_size, sorted=True)
passage_emb = defaultdict(float)
for score, idx in zip(top_results[0].cpu().tolist(), top_results[1].cpu().tolist()):
if score > 0:
passage_emb[idx] = score
else:
break
sparse_embeddings.append(passage_emb)
return sparse_embeddings
def evaluate_msmarco():
passage_embs_sorted = []
batch_size = 32
length_sorted_idx = np.argsort([-len(pas) for pas in dev_corpus])
dev_corpus_sorted = [dev_corpus[idx] for idx in length_sorted_idx]
for start_idx in tqdm.trange(0, len(dev_corpus_sorted), batch_size, desc='encode corpus'):
passage_embs_sorted.extend(compute_passage_emb(dev_corpus_sorted[start_idx:start_idx + batch_size]))
passage_embs = [passage_embs_sorted[idx] for idx in np.argsort(length_sorted_idx)]
logging.info("Create sparse matrix")
row = []
col = []
values = []
for pid, emb in enumerate(passage_embs):
for tid, score in emb.items():
row.append(tid)
col.append(pid)
values.append(score)
sparse = csr_matrix((values, (row, col)), shape=(len(model.tokenizer), len(passage_embs)), dtype=np.float)
logging.info("Scores: {}".format(sparse.shape))
mrr = []
k = 10
for qid, question in tqdm.tqdm(dev_queries.items(), desc="score"):
token_ids = model.tokenizer(question, add_special_tokens=False)['input_ids']
# Get the candidate passages
scores = np.asarray(sparse[token_ids, :].sum(axis=0)).squeeze(0)
top_k_ind = np.argpartition(scores, -k)[-k:]
hits = sorted([(dev_corpus_pids[pid], scores[pid]) for pid in top_k_ind], key=lambda x: x[1], reverse=True)
mrr_score = 0
for rank, hit in enumerate(hits[0:10]):
pid = hit[0]
if pid in dev_rel_docs[qid]:
mrr_score = 1 / (rank + 1)
break
mrr.append(mrr_score)
assert len(mrr) == len(dev_queries)
mrr = np.mean(mrr)
logging.info("MRR@10: {:.4f}".format(mrr))
return mrr
best_score = 0 #evaluate_msmarco()
#################
#### Read train file
with gzip.open('../data/collection.tsv.gz', 'rt') as fIn:
for line in fIn:
pid, passage = line.strip().split("\t")
corpus[pid] = passage
with open('../data/queries.train.tsv', 'r') as fIn:
for line in fIn:
qid, query = line.strip().split("\t")
train_queries[qid] = {'query': query,
'pos': set(),
'soft-pos': set(),
'neg': set()}
#Read qrels file for relevant positives per query
with open('../data/qrels.train.tsv') as fIn:
for line in fIn:
qid, _, pid, _ = line.strip().split()
train_queries[qid]['pos'].add(pid)
logging.info("Clean train queries")
deleted_queries = 0
for qid in list(train_queries.keys()):
if len(train_queries[qid]['pos']) == 0:
deleted_queries += 1
del train_queries[qid]
continue
logging.info("Deleted queries pos-empty: {}".format(deleted_queries))
for hard_neg_file in ['../data/hard-negatives-all.jsonl.gz']: #'../data/hard-negatives-ann-roberta.jsonl.gz']: #['../data/hard-negatives-ann-msmarco-distilbert-base-v2.jsonl.gz', '../data/hard-negatives-ann.jsonl.gz', '../data/hard-negatives-ann-no_idnt.jsonl.gz', '../data/hard-negatives-all.jsonl.gz']:
logging.info("Read hard negatives: "+hard_neg_file)
with gzip.open(hard_neg_file, 'rt') as fIn:
try:
for line in fIn:
try:
data = json.loads(line)
except:
continue
qid = data['qid']
if qid in train_queries:
neg_added = 0
max_neg_added = 100
hits = sorted(data['hits'], key=lambda x: x['score'] if 'score' in x else x['bm25-score'], reverse=True)
for hit in hits:
pid = hit['corpus_id'] if 'corpus_id' in hit else hit['pid']
if pid in train_queries[qid]['pos']: #Skip entries we have as positives
continue
if hit['bert-score'] < 0.1 and neg_added < max_neg_added:
train_queries[qid]['neg'].add(pid)
neg_added += 1
elif hit['bert-score'] > 0.9:
train_queries[qid]['soft-pos'].add(pid)
except:
pass
logging.info("Clean train queries with empty neg set")
deleted_queries = 0
for qid in list(train_queries.keys()):
if len(train_queries[qid]['neg']) == 0:
deleted_queries += 1
del train_queries[qid]
continue
logging.info("Deleted queries neg empty: {}".format(deleted_queries))
train_queries = list(train_queries.values())
for idx in range(len(train_queries)):
train_queries[idx]['pos'] = list(train_queries[idx]['pos'])
train_queries[idx]['neg'] = list(train_queries[idx]['neg'])
train_queries[idx]['soft-pos'] = list(train_queries[idx]['soft-pos'])
###########################################
####
# Prepare optimizers
param_optimizer = list(model.named_parameters())
no_decay = ['bias', 'LayerNorm.bias', 'LayerNorm.weight']
optimizer_grouped_parameters = [
{'params': [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)], 'weight_decay': 0.01},
{'params': [p for n, p in param_optimizer if any(nd in n for nd in no_decay)], 'weight_decay': 0.0}
]
grad_acc_steps, lr = 1, 2e-5
#grad_acc_steps, lr = 16, 2e-5
num_epochs = 1
optimizer = transformers.AdamW(model.parameters(), lr=lr, eps=1e-6) #optimizer_grouped_parameters
t_total = math.ceil(len(train_queries)/batch_size*num_epochs)
num_warmup_steps = int(t_total/grad_acc_steps * 0.1) #10% for warm up
scheduler = transformers.get_linear_schedule_with_warmup(optimizer, num_warmup_steps=num_warmup_steps, num_training_steps=t_total)
loss_fct = torch.nn.CrossEntropyLoss()
max_grad_norm = 1
for epoch in tqdm.trange(num_epochs, desc='Epochs'):
random.shuffle(train_queries)
idx = 0
for start_idx in tqdm.trange(0, len(train_queries), batch_size):
idx += 1
if (idx) % 5000 == 0:
score = evaluate_msmarco()
if score > best_score:
best_score = score
model.bert_model.save_pretrained(model_save_path)
logging.info(f"Save to {model_save_path}")
batch = train_queries[start_idx:start_idx+batch_size]
queries = [b['query'] for b in batch]
#First the positives
passages = [corpus[random.choice(b['pos'])] for b in batch]
#Then the negatives
for b in batch:
for pid in random.sample(b['neg'], k=min(len(b['neg']), num_negatives)):
passages.append(corpus[pid])
label = torch.tensor(list(range(len(batch))), device=device)
##FP16
with autocast():
final_scores = model(queries, passages)
final_scores = 5*final_scores
loss_value = loss_fct(final_scores, label) / grad_acc_steps
scaler.scale(loss_value).backward()
if (idx + 1) % grad_acc_steps == 0:
scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
scaler.step(optimizer)
scaler.update()
model.zero_grad()
scheduler.step()
"""
#Normal FP32 with grad acc
final_scores = model(query, passages)
#Compute loss
loss_value = loss_fct(final_scores, label)
if grad_acc_steps > 1:
loss_value /= grad_acc_steps
loss_value.backward()
if (idx+1) % grad_acc_steps == 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_grad_norm)
optimizer.step()
model.zero_grad()
scheduler.step()
"""
logging.info("Final eval:")
evaluate_msmarco()
# Script was called via:
#python train_sparta_msmarco.py distilbert-base-uncased no weight decay, 5* score scaling |