-
Notifications
You must be signed in to change notification settings - Fork 4
/
run.py
1230 lines (1049 loc) · 49.5 KB
/
run.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
from abc import ABC
from transformers import T5EncoderModel, AutoTokenizer, AutoModelForSeq2SeqLM
from torch.nn.utils.rnn import pad_sequence
from transformers import Adafactor, get_linear_schedule_with_warmup, get_constant_schedule
from torch.optim import AdamW
from accelerate import Accelerator
from torch.utils.data import Dataset
from transformers import TrainingArguments, Trainer
from transformers.trainer_pt_utils import get_parameter_names
from transformers.generation_utils import GenerationMixin
from torch import nn, Tensor
import torch.distributed as dist
from typing import Optional, Union, List, Dict, Any, Tuple
from tqdm import tqdm
from dataclasses import dataclass, field
from transformers.modeling_outputs import ModelOutput
import torch.nn.functional as F
from utils.io import read_pkl, write_pkl, read_file
from collections import defaultdict
from copy import deepcopy
import numpy as np
import json
import faiss
import torch
import os
import argparse
import time
from tqdm import tqdm
import torch
class Tree:
def __init__(self):
self.root = dict()
def set(self, path):
pointer = self.root
for i in path:
if i not in pointer:
pointer[i] = dict()
pointer = pointer[i]
def set_all(self, path_list):
for path in tqdm(path_list):
self.set(path)
def find(self, path):
if isinstance(path, torch.Tensor):
path = path.cpu().tolist()
pointer = self.root
for i in path:
if i not in pointer:
return []
pointer = pointer[i]
return list(pointer.keys())
def __call__(self, batch_id, path):
return self.find(path)
@dataclass
class QuantizeOutput(ModelOutput):
logits: Optional[torch.FloatTensor] = None
all_dense_embed: Optional[torch.FloatTensor] = None
continuous_embeds: Optional[torch.FloatTensor] = None
quantized_embeds: Optional[torch.FloatTensor] = None
discrete_codes: Optional[torch.LongTensor] = None
probability: Optional[torch.FloatTensor] = None
code_logits: Optional[torch.FloatTensor] = None
@torch.no_grad()
def sinkhorn_algorithm(out: Tensor, epsilon: float,
sinkhorn_iterations: int,
use_distrib_train: bool):
Q = torch.exp(out / epsilon) # Q is M-K-by-B
M = Q.shape[0]
B = Q.shape[2] # number of samples to assign
K = Q.shape[1] # how many centroids per block (usually set to 256)
# make the matrix sums to 1
sum_Q = Q.sum(-1, keepdim=True).sum(-2, keepdim=True)
if use_distrib_train:
B *= dist.get_world_size()
dist.all_reduce(sum_Q)
Q /= sum_Q
for it in range(sinkhorn_iterations):
# normalize each row: total weight per prototype must be 1/K
sum_of_rows = torch.sum(Q, dim=2, keepdim=True)
if use_distrib_train:
dist.all_reduce(sum_of_rows)
Q /= sum_of_rows
Q /= K
# normalize each column: total weight per sample must be 1/B
Q /= torch.sum(Q, dim=1, keepdim=True)
Q /= B
Q *= B # the colomns must sum to 1 so that Q is an assignment
return Q
@torch.no_grad()
def sinkhorn_raw(out: Tensor, epsilon: float,
sinkhorn_iterations: int,
use_distrib_train: bool):
Q = torch.exp(out / epsilon).t() # Q is K-by-B for consistency with notations from our paper
B = Q.shape[1]
K = Q.shape[0] # how many prototypes
# make the matrix sums to 1
sum_Q = torch.clamp(torch.sum(Q), min=1e-5)
if use_distrib_train:
B *= dist.get_world_size()
dist.all_reduce(sum_Q)
Q /= sum_Q
for it in range(sinkhorn_iterations):
# normalize each row: total weight per prototype must be 1/K
sum_of_rows = torch.clamp(torch.sum(Q, dim=1, keepdim=True), min=1e-5)
if use_distrib_train:
dist.all_reduce(sum_of_rows)
Q /= sum_of_rows
Q /= K
# normalize each column: total weight per sample must be 1/B
Q /= torch.clamp(torch.sum(torch.sum(Q, dim=0, keepdim=True), dim=1, keepdim=True), min=1e-5)
Q /= B
Q *= B
return Q.t()
def get_optimizer(model, lr):
decay_parameters = get_parameter_names(model, [nn.LayerNorm])
decay_parameters = [name for name in decay_parameters if "bias" not in name]
optimizer_grouped_parameters = [
{
"params": [p for n, p in model.named_parameters() if n in decay_parameters and "centroids" not in n],
"weight_decay": 0.0,
},
{
"params": [p for n, p in model.named_parameters() if
n not in decay_parameters and "centroids" not in n],
"weight_decay": 0.0,
},
{
"params": [p for n, p in model.named_parameters() if "centroids" in n],
"weight_decay": 0.0,
'lr': lr * 20
},
]
optimizer = AdamW(optimizer_grouped_parameters, lr)
return optimizer
class Model(nn.Module, GenerationMixin, ABC):
def __init__(self, model, use_constraint: bool, sk_epsilon: float = 0.03, sk_iters: int = 100, code_length=1,
zero_inp=False, code_number=10):
super().__init__()
self.model = model
self.config = model.config
self.generation_config = model.generation_config
self.main_input_name = model.main_input_name
self.get_encoder = model.get_encoder
self.device = model.device
self.prepare_inputs_for_generation = model.prepare_inputs_for_generation
self.can_generate = lambda: True
hidden_size = model.config.hidden_size
self.use_constraint, self.sk_epsilon, self.sk_iters = use_constraint, sk_epsilon, sk_iters
# Codebook of each time step
self.centroids = nn.ModuleList([nn.Linear(hidden_size, code_number, bias=False) for _ in range(code_length)])
self.centroids.requires_grad_(True)
# Code embedding (input to the decoder)
self.code_embedding = nn.ModuleList([nn.Embedding(code_number, hidden_size) for _ in range(code_length)])
self.code_embedding.requires_grad_(True)
self.code_length = code_length
self.zero_inp = zero_inp
self.code_number = code_number
def prepare_inputs_for_generation(self, input_ids, attention_mask=None, encoder_outputs=None, **kwargs):
return {"decoder_input_ids": input_ids, "encoder_outputs": encoder_outputs, "attention_mask": attention_mask}
@torch.no_grad()
def quantize(self, probability, use_constraint=None):
# batchsize_per_device = len(continuous_embeds)
# distances = ((continuous_embeds.reshape(batchsize_per_device, self.config.MCQ_M, 1, -1).transpose(0,1) -
# self.centroids.unsqueeze(1)) ** 2).sum(-1) # M, bs', K
distances = -probability
use_constraint = self.use_constraint if use_constraint is None else use_constraint
# raw_code = torch.argmin(distances, dim=-1)
# print('In', torch.argmin(distances, dim=-1))
if not use_constraint:
codes = torch.argmin(distances, dim=-1) # M, bs
else:
distances = self.center_distance_for_constraint(distances) # to stablize
# avoid nan
distances = distances.double()
# Q = sinkhorn_algorithm(
# -distances.transpose(1, 2),
# self.sk_epsilon,
# self.sk_iters,
# use_distrib_train=dist.is_initialized()
# ).transpose(1, 2) # M-B-K
Q = sinkhorn_raw(
-distances,
self.sk_epsilon,
self.sk_iters,
use_distrib_train=dist.is_initialized()
) # B-K
codes = torch.argmax(Q, dim=-1)
if torch.isnan(Q).any() or torch.isinf(Q).any():
print(f"Sinkhorn Algorithm returns nan/inf values.")
# print('Out', codes)
# print('Equal', (raw_code == codes).float().mean())
# codes = codes.t() # bs, M
# input('>')
return codes
def decode(self, codes, centroids=None):
M = codes.shape[1]
if centroids is None:
centroids = self.centroids
if isinstance(codes, torch.Tensor):
assert isinstance(centroids, torch.Tensor)
first_indices = torch.arange(M).to(codes.device)
first_indices = first_indices.expand(*codes.shape).reshape(-1)
quant_embeds = centroids[first_indices, codes.reshape(-1)].reshape(len(codes), -1)
elif isinstance(codes, np.ndarray):
if isinstance(centroids, torch.Tensor):
centroids = centroids.detach().cpu().numpy()
first_indices = np.arange(M)
first_indices = np.tile(first_indices, len(codes))
quant_embeds = centroids[first_indices, codes.reshape(-1)].reshape(len(codes), -1)
else:
raise NotImplementedError()
return quant_embeds
def embed_decode(self, codes, centroids=None):
if centroids is None:
centroids = self.centroids[-1]
quant_embeds = F.embedding(codes, centroids.weight)
return quant_embeds
@staticmethod
def center_distance_for_constraint(distances):
# distances: M, bs, K
max_distance = distances.max()
min_distance = distances.min()
if dist.is_initialized():
dist.all_reduce(max_distance, torch.distributed.ReduceOp.MAX)
dist.all_reduce(min_distance, torch.distributed.ReduceOp.MIN)
middle = (max_distance + min_distance) / 2
amplitude = max_distance - middle + 1e-5
assert torch.all(amplitude > 0)
centered_distances = (distances - middle) / amplitude
return centered_distances
def forward(self, input_ids=None, attention_mask=None, decoder_input_ids=None, aux_ids=None, return_code=False,
return_quantized_embedding=False, use_constraint=None, encoder_outputs=None, **kwargs):
if decoder_input_ids is None or self.zero_inp:
decoder_input_ids = torch.zeros(input_ids.size(0), self.code_length).long().to(input_ids.device)
# decoder_inputs_embeds = self.code_embedding(decoder_input_ids)
decoder_inputs_embeds = []
for i in range(min(decoder_input_ids.size(1), len(self.code_embedding))):
code_embedding = self.code_embedding[i]
decoder_inputs_embeds.append(code_embedding(decoder_input_ids[:, i]))
decoder_inputs_embeds = torch.stack(decoder_inputs_embeds, dim=1)
model_outputs = self.model(
input_ids=input_ids,
attention_mask=attention_mask,
# decoder_input_ids=decoder_input_ids,
decoder_inputs_embeds=decoder_inputs_embeds,
output_hidden_states=True,
encoder_outputs=encoder_outputs
)
decoder_outputs = model_outputs.decoder_hidden_states[-1]
all_dense_embed = decoder_outputs.view(decoder_outputs.size(0), -1).contiguous()
dense_embed = decoder_outputs[:, -1].contiguous()
code_logits = []
for i in range(min(decoder_input_ids.size(1), len(self.code_embedding))):
centroid = self.centroids[i]
code_logits.append(centroid(decoder_outputs[:, i]))
code_logits = torch.stack(code_logits, dim=1)
# code_logits = self.centroids(decoder_outputs)
probability = code_logits[:, -1].contiguous()
# probability = torch.mm(dense_embed, self.centroids.transpose(0, 1))
discrete_codes = self.quantize(probability, use_constraint=use_constraint)
if aux_ids is None:
aux_ids = discrete_codes
quantized_embeds = self.embed_decode(aux_ids) if return_quantized_embedding else None
if self.code_length == 1:
return_code_logits = None
else:
return_code_logits = code_logits[:, :-1].contiguous()
quant_output = QuantizeOutput(
logits=code_logits,
all_dense_embed=all_dense_embed,
continuous_embeds=dense_embed,
quantized_embeds=quantized_embeds,
discrete_codes=discrete_codes,
probability=probability,
code_logits=return_code_logits,
)
return quant_output
class OurTrainer:
@staticmethod
def _gather_tensor(t: Tensor, local_rank):
all_tensors = [torch.empty_like(t) for _ in range(dist.get_world_size())]
dist.all_gather(all_tensors, t)
all_tensors[local_rank] = t
return all_tensors
@staticmethod
def gather_tensors(t: Tensor, local_rank=None):
if local_rank is None:
local_rank = dist.get_rank()
return torch.cat(OurTrainer._gather_tensor(t, local_rank))
@staticmethod
@torch.no_grad()
def test_step(model: Model, batch, use_constraint=None):
query_outputs: QuantizeOutput = model(input_ids=batch['query'], attention_mask=batch['query'].ne(0),
decoder_input_ids=batch['ids'],
aux_ids=None, return_code=False,
return_quantized_embedding=False, use_constraint=use_constraint)
doc_outputs: QuantizeOutput = model(input_ids=batch['doc'], attention_mask=batch['doc'].ne(0),
decoder_input_ids=batch['ids'],
aux_ids=None, return_code=False,
return_quantized_embedding=False, use_constraint=use_constraint)
return query_outputs, doc_outputs
@staticmethod
def simple_train_step(model: Model, batch, gathered=None):
query_outputs: QuantizeOutput = model(input_ids=batch['query'], attention_mask=batch['query'].ne(0),
decoder_input_ids=batch['ids'])
# doc_outputs: QuantizeOutput = model(input_ids=batch['doc'], attention_mask=batch['doc'].ne(0),
# decoder_input_ids=batch['ids'])
if isinstance(model, torch.nn.parallel.DistributedDataParallel):
code_number = model.module.code_number
else:
code_number = model.code_number
# code_number = 10
query_code_loss = F.cross_entropy(query_outputs.code_logits.view(-1, code_number),
batch['ids'][:, 1:].reshape(-1))
# doc_code_loss = F.cross_entropy(doc_outputs.code_logits.view(-1, code_number),
# batch['ids'][:, 1:].reshape(-1))
query_prob = query_outputs.probability
aux_query_code_loss = F.cross_entropy(query_prob, batch['aux_ids'])
code_loss = query_code_loss
return dict(
loss=query_code_loss + aux_query_code_loss,
)
@staticmethod
def train_step(model: Model, batch, gathered=None):
query_outputs: QuantizeOutput = model(input_ids=batch['query'], attention_mask=batch['query'].ne(0),
decoder_input_ids=batch['ids'],
aux_ids=batch['aux_ids'], return_code=True,
return_quantized_embedding=True)
doc_outputs: QuantizeOutput = model(input_ids=batch['doc'], attention_mask=batch['doc'].ne(0),
decoder_input_ids=batch['ids'],
aux_ids=batch['aux_ids'], return_code=True,
return_quantized_embedding=True)
query_embeds = query_outputs.continuous_embeds
doc_embeds = doc_outputs.continuous_embeds
codes_doc = doc_outputs.discrete_codes
quant_doc_embeds = doc_outputs.quantized_embeds
query_prob = query_outputs.probability
doc_prob = doc_outputs.probability
query_all_embeds = query_outputs.all_dense_embed
doc_all_embeds = doc_outputs.all_dense_embed
if gathered is None:
gathered = dist.is_initialized()
cl_loss = OurTrainer.compute_contrastive_loss(query_embeds, doc_embeds, gathered=False) # retrieval
all_cl_loss = OurTrainer.compute_contrastive_loss(query_all_embeds, doc_all_embeds,
gathered=dist.is_initialized()) # retrieval (used when dist)
# cl_d_loss = OurTrainer.compute_contrastive_loss(doc_embeds, query_embeds, gathered=gathered)
# cl_loss = cl_q_loss + cl_d_loss
# mse_loss = 0
cl_dd_loss = OurTrainer.compute_contrastive_loss(
quant_doc_embeds + doc_embeds - doc_embeds.detach(), doc_embeds.detach(), gathered=False) # reconstruction
# mse_loss = ((quant_doc_embeds - doc_embeds.detach()) ** 2).sum(-1).mean()
# codes_doc_cpu = codes_doc.cpu().tolist()
# print(balance(codes_doc_cpu))
# print(codes_doc)
query_ce_loss = F.cross_entropy(query_prob, codes_doc.detach()) # commitment
doc_ce_loss = F.cross_entropy(doc_prob, codes_doc.detach()) # commitment
ce_loss = query_ce_loss + doc_ce_loss # commitment
code_loss = 0
if query_outputs.code_logits is not None:
if isinstance(model, torch.nn.parallel.DistributedDataParallel):
code_number = model.module.code_number
else:
code_number = model.code_number
query_code_loss = F.cross_entropy(query_outputs.code_logits.view(-1, code_number),
batch['ids'][:, 1:].reshape(-1))
doc_code_loss = F.cross_entropy(doc_outputs.code_logits.view(-1, code_number),
batch['ids'][:, 1:].reshape(-1))
code_loss = query_code_loss + doc_code_loss # commitment
if batch['aux_ids'] is not None:
aux_query_code_loss = F.cross_entropy(query_prob, batch['aux_ids'])
aux_doc_code_loss = F.cross_entropy(doc_prob, batch['aux_ids'])
aux_code_loss = aux_query_code_loss + aux_doc_code_loss # commitment on last token
# print('Q', aux_query_code_loss.item(), 'D', aux_doc_code_loss.item())
if aux_code_loss.isnan():
aux_code_loss = 0
else:
aux_code_loss = 0
if dist.is_initialized():
all_doc_embeds = OurTrainer.gather_tensors(doc_embeds)
global_mean_doc_embeds = all_doc_embeds.mean(dim=0)
local_mean_doc_embeds = doc_embeds.mean(dim=0)
clb_loss = F.mse_loss(local_mean_doc_embeds, global_mean_doc_embeds.detach()) # not used
else:
clb_loss = 0
return dict(
cl_loss=cl_loss,
all_cl_loss=all_cl_loss,
mse_loss=0,
ce_loss=ce_loss,
code_loss=code_loss,
aux_code_loss=aux_code_loss,
cl_dd_loss=cl_dd_loss,
clb_loss=clb_loss
)
@staticmethod
def compute_contrastive_loss(query_embeds, doc_embeds, gathered=True):
if gathered:
gathered_query_embeds = OurTrainer.gather_tensors(query_embeds)
gathered_doc_embeds = OurTrainer.gather_tensors(doc_embeds)
else:
gathered_query_embeds = query_embeds
gathered_doc_embeds = doc_embeds
effective_bsz = gathered_query_embeds.size(0)
labels = torch.arange(effective_bsz, dtype=torch.long, device=query_embeds.device)
similarities = torch.matmul(gathered_query_embeds, gathered_doc_embeds.transpose(0, 1))
# similarities = similarities
co_loss = F.cross_entropy(similarities, labels)
return co_loss
class BiDataset(Dataset, ABC):
def __init__(self, data, corpus, tokenizer, max_doc_len=512, max_q_len=128, ids=None, batch_size=1, aux_ids=None):
self.data = data
self.corpus = corpus
self.tokenizer = tokenizer
self.max_doc_len = max_doc_len
self.max_q_len = max_q_len
self.ids = ids
self.batch_size = batch_size
if self.batch_size != 1:
ids_to_item = defaultdict(list)
for i, item in enumerate(self.data):
ids_to_item[str(ids[item[1]])].append(i)
for key in ids_to_item:
np.random.shuffle(ids_to_item[key])
self.ids_to_item = ids_to_item
else:
self.ids_to_item = None
self.aux_ids = aux_ids
def getitem(self, item):
queries, doc_id = self.data[item]
if isinstance(queries, list):
query = np.random.choice(queries)
else:
query = queries
while isinstance(doc_id, list):
doc_id = doc_id[0]
doc = self.corpus[doc_id]
if self.ids is None:
ids = [0]
else:
ids = self.ids[doc_id]
if self.aux_ids is None:
aux_ids = -100
else:
aux_ids = self.aux_ids[doc_id]
return (torch.tensor(self.tokenizer.encode(query, truncation=True, max_length=self.max_q_len)),
torch.tensor(self.tokenizer.encode(doc, truncation=True, max_length=self.max_doc_len)),
ids, aux_ids)
def __getitem__(self, item):
if self.batch_size == 1:
return [self.getitem(item)]
else:
# item_set = self.ids_to_item[str(self.ids[self.data[item][1]])]
# new_item_set = [item] + [i for i in item_set if i != item]
# work_item_set = new_item_set[:self.batch_size]
# new_item_set = new_item_set[self.batch_size:] + work_item_set
# self.ids_to_item[str(self.ids[self.data[item][1]])] = new_item_set
item_set = deepcopy(self.ids_to_item[str(self.ids[self.data[item][1]])])
np.random.shuffle(item_set)
item_set = [item] + [i for i in item_set if i != item]
work_item_set = item_set[:self.batch_size]
if len(work_item_set) < self.batch_size:
rand_item_set = np.random.randint(len(self), size=self.batch_size * 2)
rand_item_set = [i for i in rand_item_set if i != item]
work_item_set = work_item_set + rand_item_set
work_item_set = work_item_set[:self.batch_size]
collect = []
for item in work_item_set:
query, doc, ids, aux_ids = self.getitem(item)
collect.append((query, doc, ids, aux_ids))
return collect
def __len__(self):
return len(self.data)
def collate_fn(self, data):
data = sum(data, [])
query, doc, ids, aux_ids = zip(*data)
query = pad_sequence(query, batch_first=True, padding_value=0)
doc = pad_sequence(doc, batch_first=True, padding_value=0)
ids = torch.tensor(ids)
if self.aux_ids is None:
aux_ids = None
else:
aux_ids = torch.tensor(aux_ids)
return {
'query': query,
'doc': doc,
'ids': ids,
'aux_ids': aux_ids
}
def safe_load(model, file):
state_dict = torch.load(file, map_location=lambda storage, loc: storage)
model_state_dict_keys = list(model.state_dict().keys())
new_state_dict_keys = list(state_dict.keys())
new_keys_in_new = [k for k in new_state_dict_keys if k not in model_state_dict_keys]
no_match_keys_of_model = [k for k in model_state_dict_keys if k not in new_state_dict_keys]
# size_not_match = [k for k,v in state_dict.items() if model_state_dict_keys[k]]
print('##', model._get_name(), '# new keys in file:', new_keys_in_new, '# no match keys:', no_match_keys_of_model)
model.load_state_dict(state_dict, strict=False)
def safe_load_embedding(model, file):
state_dict = torch.load(file, map_location=lambda storage, loc: storage)
model_state_dict_keys = list(model.state_dict().keys())
new_state_dict_keys = list(state_dict.keys())
new_keys_in_new = [k for k in new_state_dict_keys if k not in model_state_dict_keys]
no_match_keys_of_model = [k for k in model_state_dict_keys if k not in new_state_dict_keys]
print('##', model._get_name(), '# new keys in file:', new_keys_in_new, '# no match keys:', no_match_keys_of_model)
matched_state_dict = deepcopy(model.state_dict())
for key in model_state_dict_keys:
if key in state_dict:
file_size = state_dict[key].size(0)
model_embedding = matched_state_dict[key].clone()
model_size = model_embedding.size(0)
model_embedding[:file_size, :] = state_dict[key][:model_size, :]
matched_state_dict[key] = model_embedding
print(f'Copy {key} {matched_state_dict[key].size()} from {state_dict[key].size()}')
model.load_state_dict(matched_state_dict, strict=False)
def safe_save(accelerator, model, save_path, epoch, end_epoch=100, save_step=1, last_checkpoint=None):
os.makedirs(save_path, exist_ok=True)
accelerator.wait_for_everyone()
if accelerator.is_local_main_process and epoch < end_epoch and epoch % save_step == 0:
unwrap_model = accelerator.unwrap_model(model)
accelerator.save(unwrap_model.state_dict(), f'{save_path}/{epoch}.pt')
accelerator.save(unwrap_model.model.state_dict(), f'{save_path}/{epoch}.pt.model')
accelerator.save(unwrap_model.centroids.state_dict(), f'{save_path}/{epoch}.pt.centroids')
accelerator.save(unwrap_model.code_embedding.state_dict(), f'{save_path}/{epoch}.pt.embedding')
accelerator.print(f'Save model {save_path}/{epoch}.pt')
last_checkpoint = f'{save_path}/{epoch}.pt'
return epoch + 1, last_checkpoint
def simple_loader(data, corpus, tokenizer, ids, aux_ids, accelerator):
dataset = BiDataset(data=data, corpus=corpus, tokenizer=tokenizer,
max_doc_len=128, max_q_len=32, ids=ids, batch_size=1, aux_ids=aux_ids)
data_loader = torch.utils.data.DataLoader(dataset, collate_fn=dataset.collate_fn, batch_size=32,
shuffle=True, num_workers=4)
data_loader = accelerator.prepare(data_loader)
return data_loader
def train(config):
accelerator = Accelerator(gradient_accumulation_steps=1)
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
model_name = config.get('model_name', 't5-base')
code_num = config.get('code_num', 512)
code_length = config.get('code_length', 1)
prev_model = config.get('prev_model', None)
prev_id = config.get('prev_id', None)
save_path = config.get('save_path', None)
train_data = config.get('train_data', 'dataset/nq320k/train.json')
corpus_data = config.get('corpus_data', 'dataset/nq320k/corpus_lite.json')
epochs = config.get('epochs', 100)
in_batch_size = config.get('batch_size', 128)
end_epoch = epochs
os.environ['TOKENIZERS_PARALLELISM'] = 'false'
save_step = 1
batch_size = 1
lr = 5e-4
accelerator.print(save_path)
t5 = AutoModelForSeq2SeqLM.from_pretrained(model_name)
model = Model(model=t5, code_length=code_length,
use_constraint=True, sk_epsilon=1, zero_inp=False, code_number=code_num)
if prev_model is not None:
safe_load(model.model, f'{prev_model}.model')
safe_load(model.centroids, f'{prev_model}.centroids')
safe_load_embedding(model.code_embedding, f'{prev_model}.embedding')
if config.get('codebook_init', None) is not None:
model.centroids[0].weight.data = torch.tensor(read_pkl(config.get('codebook_init')))
for i in range(code_length - 1):
model.centroids[i].requires_grad_(False)
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
data = json.load(open(train_data))
data.extend(json.load(open(f'{train_data}.qg.json')))
corpus = json.load(open(corpus_data))
grouped_data = defaultdict(list)
for i, item in enumerate(data):
query, docid = item
if isinstance(docid, list):
docid = docid[0]
grouped_data[docid].append(query)
data = [[query_list, docid] for docid, query_list in grouped_data.items()]
ids, aux_ids = None, None
if prev_id is not None:
ids = [[0, *line] for line in json.load(open(prev_id))]
else:
ids = [[0]] * len(corpus)
dataset = BiDataset(data=data, corpus=corpus, tokenizer=tokenizer,
max_doc_len=128, max_q_len=32, ids=ids, batch_size=in_batch_size, aux_ids=aux_ids)
accelerator.print(f'data size={len(dataset)}')
data_loader = torch.utils.data.DataLoader(dataset, collate_fn=dataset.collate_fn, batch_size=batch_size,
shuffle=True, num_workers=2)
optimizer = AdamW(model.parameters(), lr)
# optimizer = get_optimizer(model, lr=lr)
model, optimizer, data_loader = accelerator.prepare(model, optimizer, data_loader)
scheduler = get_constant_schedule(optimizer)
w_1 = {'cl_loss': 0.5, 'all_cl_loss': 0, 'ce_loss': 0, 'code_loss': 0.5, 'aux_code_loss': 0, 'mse_loss': 0,
'cl_dd_loss': 0, 'clb_loss': 0}
w_2 = {'cl_loss': 0.5, 'all_cl_loss': 0, 'ce_loss': 0.5, 'code_loss': 0.5, 'aux_code_loss': 0, 'mse_loss': 0,
'cl_dd_loss': 0.1, 'clb_loss': 0}
w_3 = {'cl_loss': 0, 'all_cl_loss': 0, 'ce_loss': 0.5, 'code_loss': 1, 'aux_code_loss': 0, 'mse_loss': 0,
'cl_dd_loss': 0, 'clb_loss': 0}
loss_w = [None, w_1, w_2, w_3][config['loss_w']]
step, epoch = 0, 0
epoch_step = len(data_loader) // in_batch_size
# safe_save(accelerator, model, save_path, -1, end_epoch=end_epoch)
last_checkpoint = None
for _ in range(epochs):
accelerator.print(f'Training {save_path} {epoch}')
accelerator.wait_for_everyone()
model.train()
tk0 = tqdm(data_loader, total=len(data_loader))
loss_report = []
for batch in tk0:
step += 1
with accelerator.accumulate(model):
# losses = OurTrainer.train_step(model, batch, gathered=False)
# loss = sum([v * loss_w[k] for k, v in losses.items()])
# accelerator.backward(loss)
# accelerator.clip_grad_norm_(model.parameters(), 1.)
# optimizer.step()
# scheduler.step()
# optimizer.zero_grad()
#
# loss = accelerator.gather(loss).mean().item()
loss = 0.
loss_report.append(loss)
tk0.set_postfix(loss=sum(loss_report[-100:]) / len(loss_report[-100:]))
if in_batch_size != 1 and step > (epoch + 1) * epoch_step:
epoch, last_checkpoint = safe_save(accelerator, model, save_path, epoch, end_epoch=end_epoch,
save_step=save_step,
last_checkpoint=last_checkpoint)
if epoch >= end_epoch:
break
if in_batch_size == 1:
epoch = safe_save(accelerator, model, save_path, epoch, end_epoch=end_epoch, save_step=save_step)
return last_checkpoint
def balance(code, prefix=None, ncentroids=10):
if prefix is not None:
prefix = [str(x) for x in prefix]
prefix_code = defaultdict(list)
for c, p in zip(code, prefix):
prefix_code[p].append(c)
scores = []
for p, p_code in prefix_code.items():
scores.append(balance(p_code, ncentroids=ncentroids))
return {'Avg': sum(scores) / len(scores), 'Max': max(scores), 'Min': min(scores), 'Flat': balance(code)}
num = [code.count(i) for i in range(ncentroids)]
base = len(code) // ncentroids
move_score = sum([abs(j - base) for j in num])
score = 1 - move_score / len(code) / 2
return score
def conflict(code, prefix=None):
if prefix is not None:
prefix = [str(x) for x in prefix]
code = [f'{p}{c}' for c, p in zip(code, prefix)]
code = [str(c) for c in code]
freq_count = defaultdict(int)
for c in code:
freq_count[c] += 1
max_value = max(list(freq_count.values()))
min_value = min(list(freq_count.values()))
len_set = len(set(code))
return {'Max': max_value, 'Min': min_value, 'Type': len_set, '%': len_set / len(code)}
def ress(code, prefix=None):
if prefix is not None:
prefix = [str(x) for x in prefix]
code = [f'{p}{c}' for c, p in zip(code, prefix)]
freq_count = defaultdict(int)
for c in code:
freq_count[c] += 1
freq_count = [y for x, y in freq_count.items()]
freq_count.sort()
return freq_count
def ress_by_prefix(code, prefix=None):
freq_count = defaultdict(list)
for c, p in zip(code, prefix):
p = str(p)
freq_count[p].append(c)
freq_count = [[len(v), len(set(v))] for k, v in freq_count.items()]
freq_count.sort(key=lambda x: x[1])
return freq_count
def test(config):
model_name = config.get('model_name', 't5-base')
code_num = config.get('code_num', 512)
code_length = config.get('code_length', 1)
prev_id = config.get('prev_id', None)
save_path = config.get('save_path', None)
batch_size = 32
epochs = config.get('epochs', 100)
dev_data = config.get('dev_data', config.get('dev_data'))
corpus_data = config.get('corpus_data', config.get('corpus_data'))
data = json.load(open(dev_data))
corpus = json.load(open(corpus_data))
t5 = AutoModelForSeq2SeqLM.from_pretrained(model_name)
model = Model(model=t5, use_constraint=False, code_length=code_length, zero_inp=False, code_number=code_num)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# ids = None
if prev_id is not None:
corpus_ids = [[0, *line] for line in json.load(open(prev_id))]
else:
corpus_ids = [[0]] * len(corpus)
aux_ids = None
dataset = BiDataset(data=data, corpus=corpus, tokenizer=tokenizer, max_doc_len=128, max_q_len=32, ids=corpus_ids,
aux_ids=aux_ids)
data_loader = torch.utils.data.DataLoader(dataset, collate_fn=dataset.collate_fn, batch_size=batch_size,
shuffle=False, num_workers=16)
model = model.cuda()
model.eval()
seen_split = json.load(open(f'{dev_data}.seen'))
unseen_split = json.load(open(f'{dev_data}.unseen'))
for epoch in range(epochs):
if not os.path.exists(f'{save_path}/{epoch}.pt'):
continue
print(f'Test {save_path}/{epoch}.pt')
corpus_ids = [[0, *line] for line in json.load(open(f'{save_path}/{epoch}.pt.code'))]
safe_load(model, f'{save_path}/{epoch}.pt')
tree = Tree()
tree.set_all(corpus_ids)
tk0 = tqdm(data_loader, total=len(data_loader))
acc = []
output_all = []
with torch.no_grad():
for batch in tk0:
batch = {k: v.cuda() for k, v in batch.items() if v is not None}
top_k = 10
output = model.generate(
input_ids=batch['query'].cuda(),
attention_mask=batch['query'].ne(0).cuda(),
max_length=code_length + 1,
num_beams=top_k,
num_return_sequences=top_k,
prefix_allowed_tokens_fn=tree
)
beam = []
new_output = []
for line in output:
if len(beam) >= top_k:
new_output.append(beam)
beam = []
beam.append(line.cpu().tolist())
new_output.append(beam)
output_all.extend(new_output)
query_ids = [x[1] for x in data]
docid_to_doc = defaultdict(list)
for i, item in enumerate(corpus_ids):
docid_to_doc[str(item)].append(i)
predictions = []
for line in output_all:
new_line = []
for s in line:
s = str(s)
if s not in docid_to_doc:
continue
tmp = docid_to_doc[s]
# np.random.shuffle(tmp)
new_line.extend(tmp)
if len(new_line) > 100:
break
predictions.append(new_line)
from eval import eval_all
print('Test', eval_all(predictions, query_ids))
print(eval_all([predictions[j] for j in seen_split], [query_ids[j] for j in seen_split]))
print(eval_all([predictions[j] for j in unseen_split], [query_ids[j] for j in unseen_split]))
def eval_recall(predictions, labels, subset=None):
from eval import eval_all
if subset is not None:
predictions = [predictions[j] for j in subset]
labels = [labels[j] for j in subset]
labels = [[x] for x in labels]
return eval_all(predictions, labels)
@torch.no_grad()
def our_encode(data_loader, model: Model, keys='doc'):
collection = []
code_collection = []
for batch in tqdm(data_loader):
batch = {k: v.cuda() for k, v in batch.items() if v is not None}
output: QuantizeOutput = model(input_ids=batch[keys], attention_mask=batch[keys].ne(0),
decoder_input_ids=batch['ids'],
aux_ids=None, return_code=False,
return_quantized_embedding=False, use_constraint=False)
sentence_embeddings = output.continuous_embeds.cpu().tolist()
code = output.probability.argmax(-1).cpu().tolist()
code_collection.extend(code)
collection.extend(sentence_embeddings)
collection = np.array(collection, dtype=np.float32)
return collection, code_collection
def norm_by_prefix(collection, prefix):
if prefix is None:
prefix = [0 for _ in range(len(collection))]
prefix = [str(x) for x in prefix]
prefix_code = defaultdict(list)
for c, p in zip(range(len(prefix)), prefix):
prefix_code[p].append(c)
from copy import deepcopy
new_collection = deepcopy(collection)
global_mean = collection.mean(axis=0)
global_var = collection.var(axis=0)
for p, p_code in prefix_code.items():
p_collection = collection[p_code]
mean_value = p_collection.mean(axis=0)
var_value = p_collection.var(axis=0)
var_value[var_value == 0] = 1
scale = global_var / var_value
scale[np.isnan(scale)] = 1
scale = 1
p_collection = (p_collection - mean_value + global_mean) * scale
new_collection[p_code] = p_collection
return new_collection
def center_pq(m, prefix):
prefix = [str(x) for x in prefix]
prefix_code = defaultdict(list)
for c, p in zip(range(len(prefix)), prefix):
prefix_code[p].append(c)
from copy import deepcopy
new_m = deepcopy(m)
for p, p_code in prefix_code.items():
sub_m = m[p_code]
new_m[p_code] = sub_m.mean(axis=0)
return new_m
def norm_code_by_prefix(collection, centroids, prefix, epsilon=1):
if prefix is None:
prefix = [0 for _ in range(len(collection))]
attention = np.matmul(collection, centroids.T)
prefix = [str(x) for x in prefix]
prefix_code = defaultdict(list)
for c, p in zip(range(len(prefix)), prefix):
prefix_code[p].append(c)
code = [None for _ in range(len(collection))]
for p, p_code in prefix_code.items():
p_collection = attention[p_code]
distances = p_collection
max_distance = distances.max()
min_distance = distances.min()
middle = (max_distance + min_distance) / 2
amplitude = max_distance - middle + 1e-5
centered_distances = (distances - middle) / amplitude
distances = torch.tensor(centered_distances)
Q = sinkhorn_raw(
distances,
epsilon,
100,
use_distrib_train=False
) # B-K
codes = torch.argmax(Q, dim=-1).tolist()
for i, c in zip(p_code, codes):
code[i] = c
return code
def build_index(collection, shard=True, dim=None, gpu=True):
t = time.time()
dim = collection.shape[1] if dim is None else dim
cpu_index = faiss.index_factory(dim, "Flat", faiss.METRIC_INNER_PRODUCT)
# cpu_index = faiss.index_factory(dim, 'OPQ32,IVF1024,PQ32')
if gpu:
ngpus = faiss.get_num_gpus()
co = faiss.GpuMultipleClonerOptions()
co.shard = shard
gpu_index = faiss.index_cpu_to_all_gpus(cpu_index, co=co)
index = gpu_index
else:
index = cpu_index
# gpu_index.train(xb)
index.add(collection)
print(f'build index of {len(collection)} instances, time cost ={time.time() - t}')
return index
def do_retrieval(xq, index, k=1):
t = time.time()
distance, rank = index.search(xq, k)
print(f'search {len(xq)} queries, time cost ={time.time() - t}')