File size: 37,409 Bytes
5894ace
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- coding: utf-8 -*-
# @Time    : 2022/3/15 21:26
# @Author  : ruihan.wjn
# @File    : pk-plm.py

"""
This code is implemented for the paper ""Knowledge Prompting in Pre-trained Langauge Models for Natural Langauge Understanding""
"""

from time import time
import torch
from torch import nn
import torch.nn.functional as F
from torch.nn import CrossEntropyLoss
from collections import OrderedDict
from transformers.models.bert import BertPreTrainedModel, BertModel
from transformers.models.roberta import RobertaModel, RobertaPreTrainedModel, RobertaTokenizer, RobertaForMaskedLM
from transformers.models.deberta import DebertaModel, DebertaPreTrainedModel, DebertaTokenizer, DebertaForMaskedLM
from transformers.models.bert.modeling_bert import BertOnlyMLMHead, BertPreTrainingHeads
from transformers.models.roberta.modeling_roberta import RobertaModel, RobertaLMHead
from transformers.models.deberta.modeling_deberta import DebertaModel, DebertaLMPredictionHead

"""
kg enhanced corpus structure example:
{
    "token_ids": [20, 46098, 3277, 680, 10, 4066, 278, 9, 11129, 4063, 877, 579, 8, 8750, 14720, 8, 22498, 548,
    19231, 46098, 3277, 6, 25, 157, 25, 130, 3753, 46098, 3277, 4, 3684, 19809, 10960, 9, 5, 30731, 2788, 914, 5,
    1675, 8151, 35], "entity_pos": [[8, 11], [13, 15], [26, 27]],
    "entity_qid": ["Q17582", "Q231978", "Q427013"],
    "relation_pos": null,
    "relation_pid": null
}
"""


from enum import Enum
class SiameseDistanceMetric(Enum):
    """
    The metric for the contrastive loss
    """
    EUCLIDEAN = lambda x, y: F.pairwise_distance(x, y, p=2)
    MANHATTAN = lambda x, y: F.pairwise_distance(x, y, p=1)
    COSINE_DISTANCE = lambda x, y: 1-F.cosine_similarity(x, y)


class ContrastiveLoss(nn.Module):
    """
    Contrastive loss. Expects as input two texts and a label of either 0 or 1. If the label == 1, then the distance between the
    two embeddings is reduced. If the label == 0, then the distance between the embeddings is increased.
    Further information: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
    :param model: SentenceTransformer model
    :param distance_metric: Function that returns a distance between two emeddings. The class SiameseDistanceMetric contains pre-defined metrices that can be used
    :param margin: Negative samples (label == 0) should have a distance of at least the margin value.
    :param size_average: Average by the size of the mini-batch.
    Example::
        from sentence_transformers import SentenceTransformer,  SentencesDataset, LoggingHandler, losses
        from sentence_transformers.readers import InputExample
        model = SentenceTransformer("distilbert-base-nli-mean-tokens")
        train_examples = [InputExample(texts=["This is a positive pair", "Where the distance will be minimized"], label=1),
            InputExample(texts=["This is a negative pair", "Their distance will be increased"], label=0)]
        train_dataset = SentencesDataset(train_examples, model)
        train_dataloader = DataLoader(train_dataset, shuffle=True, batch_size=train_batch_size)
        train_loss = losses.ContrastiveLoss(model=model)
    """

    def __init__(self, distance_metric=SiameseDistanceMetric.COSINE_DISTANCE, margin: float = 0.5, size_average:bool = True):
        super(ContrastiveLoss, self).__init__()
        self.distance_metric = distance_metric
        self.margin = margin
        self.size_average = size_average

    def forward(self, sent_embs1, sent_embs2, labels: torch.Tensor):
        rep_anchor, rep_other = sent_embs1, sent_embs2
        distances = self.distance_metric(rep_anchor, rep_other)
        losses = 0.5 * (labels.float() * distances.pow(2) + (1 - labels).float() * F.relu(self.margin - distances).pow(2))
        return losses.mean() if self.size_average else losses.sum()



class NSPHead(nn.Module):
    def __init__(self, config):
        super().__init__()
        self.seq_relationship = nn.Linear(config.hidden_size, 2)

    def forward(self, pooled_output):
        seq_relationship_score = self.seq_relationship(pooled_output)
        return seq_relationship_score



class RoBertaKPPLMForProcessedWikiKGPLM(RobertaForMaskedLM):

    def __init__(self, config):
        super().__init__(config)
        self.num_labels = config.num_labels
        self.config = config
        # self.roberta = RobertaModel(config)
        try:
            classifier_dropout = (
                config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
            )
        except:
            classifier_dropout = (config.hidden_dropout_prob)
        self.dropout = nn.Dropout(classifier_dropout)
        # self.cls = BertOnlyMLMHead(config)
        # self.lm_head = RobertaLMHead(config)  # Masked Language Modeling head
        self.detector = NSPHead(config)  # Knowledge Noise Detection head
        self.entity_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        self.relation_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        # self.classifiers = nn.ModuleList([nn.Linear(config.hidden_size, config.num_ner_labels) for _ in range(config.entity_type_num)])

        self.contrastive_loss_fn = ContrastiveLoss()
        self.post_init()

    def forward(
            self,
            input_ids=None,
            attention_mask=None,
            token_type_ids=None,
            position_ids=None,
            head_mask=None,
            inputs_embeds=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            labels=None,
            # entity_label=None,
            entity_candidate=None,
            # relation_label=None,
            relation_candidate=None,
            noise_detect_label=None,
            task_id=None,
            mask_id=None,
            output_attentions=None,
            output_hidden_states=None,
            return_dict=None,
    ):
        # start_time = time()
        mlm_labels = labels

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        # print("attention_mask.shape=", attention_mask.shape)
        # print("input_ids[0]=", input_ids[0])
        # print("token_type_ids[0]=", token_type_ids[0])
        # attention_mask = None

        outputs = self.roberta(
            input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            encoder_hidden_states=encoder_hidden_states,
            encoder_attention_mask=encoder_attention_mask,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output = outputs[0]
        prediction_scores = self.lm_head(sequence_output)  # mlm head
        # noise_detect_scores = self.detector(pooled_output)  # knowledge noise detector use pool output
        noise_detect_scores = self.detector(sequence_output[:, 0, :])  # knowledge noise detector use cls embedding

        # ner
        # sequence_output = self.dropout(sequence_output)
        # ner_logits = torch.stack([classifier(sequence_output) for classifier in self.classifiers]).movedim(1, 0)

        # mlm
        masked_lm_loss, noise_detect_loss, entity_loss, total_loss = None, None, None, None
        total_loss = list()
        if mlm_labels is not None:
            loss_fct = CrossEntropyLoss()  # -100 index = padding token
            masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), mlm_labels.view(-1))
            total_loss.append(masked_lm_loss)

        # if noise_detect_label is not None:
        #     noise_detect_scores = noise_detect_scores[task_id == 1]
        #     noise_detect_label = noise_detect_label[task_id == 1]
        #
        #     if len(noise_detect_label) > 0:
        #         loss_fct = CrossEntropyLoss()
        #         noise_detect_loss = loss_fct(noise_detect_scores.view(-1, 2), noise_detect_label.view(-1))
        #         total_loss.append(noise_detect_loss)

        entity_candidate = entity_candidate[task_id == 2]
        if len(entity_candidate) > 0:
            batch_size = entity_candidate.shape[0]
            candidate_num = entity_candidate.shape[1]
            # print("negative_num=", negative_num)
            # 获取被mask实体的embedding
            batch_entity_query_embedding = list()
            for ei, input_id in enumerate(input_ids[task_id == 2]):
                batch_entity_query_embedding.append(
                    torch.mean(sequence_output[task_id == 2][ei][input_id == mask_id[task_id == 2][ei]], 0))  # [hidden_dim]
            batch_entity_query_embedding = torch.stack(batch_entity_query_embedding)  # [bz, dim]
            # print("batch_entity_query_embedding.shape=", batch_entity_query_embedding.shape)
            batch_entity_query_embedding = self.entity_mlp(batch_entity_query_embedding)  # [bz, dim]
            batch_entity_query_embedding = batch_entity_query_embedding.unsqueeze(1).repeat((1, candidate_num, 1))  # [bz, 11, dim]
            batch_entity_query_embedding = batch_entity_query_embedding.view(-1, batch_entity_query_embedding.shape[-1])  # [bz * 11, dim]
            # print("batch_entity_query_embedding.shape=", batch_entity_query_embedding.shape)

            # 获得positive和negative的BERT表示
            # entity_candidiate: [bz, 11, len]
            entity_candidate = entity_candidate.view(-1, entity_candidate.shape[-1])  # [bz * 11, len]
            entity_candidate_embedding = self.roberta.embeddings(input_ids=entity_candidate)  # [bz * 11, len, dim]
            entity_candidate_embedding = self.entity_mlp(torch.mean(entity_candidate_embedding, 1))  # [bz * 11, dim]

            contrastive_entity_label = torch.Tensor([0] * (candidate_num - 1) + [1]).float().cuda()
            contrastive_entity_label = contrastive_entity_label.unsqueeze(0).repeat([batch_size, 1]).view(-1)  # [bz * 11]

            entity_loss = self.contrastive_loss_fn(
                batch_entity_query_embedding, entity_candidate_embedding, contrastive_entity_label
            )
            total_loss.append(entity_loss)

        relation_candidate = relation_candidate[task_id == 3]
        if len(relation_candidate) > 0:
            batch_size = relation_candidate.shape[0]
            candidate_num = relation_candidate.shape[1]
            # print("negative_num=", negative_num)
            # 获取被mask relation的embedding
            batch_relation_query_embedding = list()
            for ei, input_id in enumerate(input_ids[task_id == 3]):
                batch_relation_query_embedding.append(
                    torch.mean(sequence_output[task_id == 3][ei][input_id == mask_id[task_id == 3][ei]], 0))  # [hidden_dim]
            batch_relation_query_embedding = torch.stack(batch_relation_query_embedding)  # [bz, dim]
            # print("batch_relation_query_embedding.shape=", batch_relation_query_embedding.shape)
            batch_relation_query_embedding = self.relation_mlp(batch_relation_query_embedding)  # [bz, dim]
            batch_relation_query_embedding = batch_relation_query_embedding.unsqueeze(1).repeat(
                (1, candidate_num, 1))  # [bz, 11, dim]
            batch_relation_query_embedding = batch_relation_query_embedding.view(-1, batch_relation_query_embedding.shape[-1])  # [bz * 11, dim]
            # print("batch_relation_query_embedding.shape=", batch_relation_query_embedding.shape)

            # 获得positive和negative的BERT表示
            # entity_candidiate: [bz, 11, len]
            relation_candidate = relation_candidate.view(-1, relation_candidate.shape[-1])  # [bz * 11, len]
            relation_candidate_embedding = self.roberta.embeddings(input_ids=relation_candidate)  # [bz * 11, len, dim]
            relation_candidate_embedding = self.relation_mlp(torch.mean(relation_candidate_embedding, 1))  # [bz * 11, dim]

            contrastive_relation_label = torch.Tensor([0] * (candidate_num - 1) + [1]).float().cuda()
            contrastive_relation_label = contrastive_relation_label.unsqueeze(0).repeat([batch_size, 1]).view(-1)  # [bz * 11]

            relation_loss = self.contrastive_loss_fn(
                batch_relation_query_embedding, relation_candidate_embedding, contrastive_relation_label
            )
            total_loss.append(relation_loss)

        total_loss = torch.sum(torch.stack(total_loss), -1)

        # end_time = time()
        # print("neural_mode_time: {}".format(end_time - start_time))
        # print("masked_lm_loss.unsqueeze(0)=", masked_lm_loss.unsqueeze(0))
        # print("masked_lm_loss.unsqueeze(0).shape=", masked_lm_loss.unsqueeze(0).shape)
        # print("logits=", prediction_scores.argmax(2))
        # print("logits.shape=", prediction_scores.argmax(2).shape)


        return OrderedDict([
            ("loss", total_loss),
            ("mlm_loss", masked_lm_loss.unsqueeze(0)),
            # ("noise_detect_loss", noise_detect_loss.unsqueeze(0) if noise_detect_loss is not None else None),
            # ("entity_loss", entity_loss.unsqueeze(0) if entity_loss is not None else None),
            # ("relation_loss", relation_loss.unsqueeze(0) if relation_loss is not None else None),
            ("logits", prediction_scores.argmax(2)),
            # ("noise_detect_logits", noise_detect_scores.argmax(-1) if noise_detect_scores is not None and len(noise_detect_scores) > 0 else None),
        ])


class DeBertaKPPLMForProcessedWikiKGPLM(DebertaForMaskedLM):

    def __init__(self, config):
        super().__init__(config)
        self.num_labels = config.num_labels
        self.config = config
        # self.roberta = RobertaModel(config)
        try:
            classifier_dropout = (
                config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
            )
        except:
            classifier_dropout = (config.hidden_dropout_prob)
        self.dropout = nn.Dropout(classifier_dropout)
        # self.cls = BertOnlyMLMHead(config)
        # self.lm_head = RobertaLMHead(config)  # Masked Language Modeling head
        self.detector = NSPHead(config)  # Knowledge Noise Detection head
        self.entity_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        self.relation_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        # self.classifiers = nn.ModuleList([nn.Linear(config.hidden_size, config.num_ner_labels) for _ in range(config.entity_type_num)])

        self.contrastive_loss_fn = ContrastiveLoss()
        self.post_init()

    def forward(
            self,
            input_ids=None,
            attention_mask=None,
            token_type_ids=None,
            position_ids=None,
            head_mask=None,
            inputs_embeds=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            labels=None,
            # entity_label=None,
            entity_candidate=None,
            # relation_label=None,
            relation_candidate=None,
            noise_detect_label=None,
            task_id=None,
            mask_id=None,
            output_attentions=None,
            output_hidden_states=None,
            return_dict=None,
    ):
        # start_time = time()
        mlm_labels = labels

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        # print("attention_mask.shape=", attention_mask.shape)
        # print("input_ids[0]=", input_ids[0])
        # print("token_type_ids[0]=", token_type_ids[0])
        # attention_mask = None

        outputs = self.deberta(
            input_ids,
            # attention_mask=attention_mask,
            attention_mask=None,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output = outputs[0]
        prediction_scores = self.cls(sequence_output)  # mlm head
        # noise_detect_scores = self.detector(pooled_output)  # knowledge noise detector use pool output
        noise_detect_scores = self.detector(sequence_output[:, 0, :])  # knowledge noise detector use cls embedding

        # ner
        # sequence_output = self.dropout(sequence_output)
        # ner_logits = torch.stack([classifier(sequence_output) for classifier in self.classifiers]).movedim(1, 0)

        # mlm
        masked_lm_loss, noise_detect_loss, entity_loss, total_loss = None, None, None, None
        total_loss = list()
        if mlm_labels is not None:
            loss_fct = CrossEntropyLoss()  # -100 index = padding token
            masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), mlm_labels.view(-1))
            total_loss.append(masked_lm_loss)

        # if noise_detect_label is not None:
        #     noise_detect_scores = noise_detect_scores[task_id == 1]
        #     noise_detect_label = noise_detect_label[task_id == 1]
        #
        #     if len(noise_detect_label) > 0:
        #         loss_fct = CrossEntropyLoss()
        #         noise_detect_loss = loss_fct(noise_detect_scores.view(-1, 2), noise_detect_label.view(-1))
        #         total_loss.append(noise_detect_loss)

        entity_candidate = entity_candidate[task_id == 2]
        if len(entity_candidate) > 0:
            batch_size = entity_candidate.shape[0]
            candidate_num = entity_candidate.shape[1]
            # print("negative_num=", negative_num)
            # 获取被mask实体的embedding
            batch_entity_query_embedding = list()
            for ei, input_id in enumerate(input_ids[task_id == 2]):
                batch_entity_query_embedding.append(
                    torch.mean(sequence_output[task_id == 2][ei][input_id == mask_id[task_id == 2][ei]], 0))  # [hidden_dim]
            batch_entity_query_embedding = torch.stack(batch_entity_query_embedding)  # [bz, dim]
            # print("batch_entity_query_embedding.shape=", batch_entity_query_embedding.shape)
            batch_entity_query_embedding = self.entity_mlp(batch_entity_query_embedding)  # [bz, dim]
            batch_entity_query_embedding = batch_entity_query_embedding.unsqueeze(1).repeat((1, candidate_num, 1))  # [bz, 11, dim]
            batch_entity_query_embedding = batch_entity_query_embedding.view(-1, batch_entity_query_embedding.shape[-1])  # [bz * 11, dim]
            # print("batch_entity_query_embedding.shape=", batch_entity_query_embedding.shape)

            # 获得positive和negative的BERT表示
            # entity_candidiate: [bz, 11, len]
            entity_candidate = entity_candidate.view(-1, entity_candidate.shape[-1])  # [bz * 11, len]
            entity_candidate_embedding = self.deberta.embeddings(input_ids=entity_candidate)  # [bz * 11, len, dim]
            entity_candidate_embedding = self.entity_mlp(torch.mean(entity_candidate_embedding, 1))  # [bz * 11, dim]

            contrastive_entity_label = torch.Tensor([0] * (candidate_num - 1) + [1]).float().cuda()
            contrastive_entity_label = contrastive_entity_label.unsqueeze(0).repeat([batch_size, 1]).view(-1)  # [bz * 11]

            entity_loss = self.contrastive_loss_fn(
                batch_entity_query_embedding, entity_candidate_embedding, contrastive_entity_label
            )
            total_loss.append(entity_loss)

        relation_candidate = relation_candidate[task_id == 3]
        if len(relation_candidate) > 0:
            batch_size = relation_candidate.shape[0]
            candidate_num = relation_candidate.shape[1]
            # print("negative_num=", negative_num)
            # 获取被mask relation的embedding
            batch_relation_query_embedding = list()
            for ei, input_id in enumerate(input_ids[task_id == 3]):
                batch_relation_query_embedding.append(
                    torch.mean(sequence_output[task_id == 3][ei][input_id == mask_id[task_id == 3][ei]], 0))  # [hidden_dim]
            batch_relation_query_embedding = torch.stack(batch_relation_query_embedding)  # [bz, dim]
            # print("batch_relation_query_embedding.shape=", batch_relation_query_embedding.shape)
            batch_relation_query_embedding = self.relation_mlp(batch_relation_query_embedding)  # [bz, dim]
            batch_relation_query_embedding = batch_relation_query_embedding.unsqueeze(1).repeat(
                (1, candidate_num, 1))  # [bz, 11, dim]
            batch_relation_query_embedding = batch_relation_query_embedding.view(-1, batch_relation_query_embedding.shape[-1])  # [bz * 11, dim]
            # print("batch_relation_query_embedding.shape=", batch_relation_query_embedding.shape)

            # 获得positive和negative的BERT表示
            # entity_candidiate: [bz, 11, len]
            relation_candidate = relation_candidate.view(-1, relation_candidate.shape[-1])  # [bz * 11, len]
            relation_candidate_embedding = self.deberta.embeddings(input_ids=relation_candidate)  # [bz * 11, len, dim]
            relation_candidate_embedding = self.relation_mlp(torch.mean(relation_candidate_embedding, 1))  # [bz * 11, dim]

            contrastive_relation_label = torch.Tensor([0] * (candidate_num - 1) + [1]).float().cuda()
            contrastive_relation_label = contrastive_relation_label.unsqueeze(0).repeat([batch_size, 1]).view(-1)  # [bz * 11]

            relation_loss = self.contrastive_loss_fn(
                batch_relation_query_embedding, relation_candidate_embedding, contrastive_relation_label
            )
            total_loss.append(relation_loss)

        total_loss = torch.sum(torch.stack(total_loss), -1)

        # end_time = time()
        # print("neural_mode_time: {}".format(end_time - start_time))
        # print("masked_lm_loss.unsqueeze(0)=", masked_lm_loss.unsqueeze(0))
        # print("masked_lm_loss.unsqueeze(0).shape=", masked_lm_loss.unsqueeze(0).shape)
        # print("logits=", prediction_scores.argmax(2))
        # print("logits.shape=", prediction_scores.argmax(2).shape)


        return OrderedDict([
            ("loss", total_loss),
            ("mlm_loss", masked_lm_loss.unsqueeze(0)),
            # ("noise_detect_loss", noise_detect_loss.unsqueeze(0) if noise_detect_loss is not None else None),
            # ("entity_loss", entity_loss.unsqueeze(0) if entity_loss is not None else None),
            # ("relation_loss", relation_loss.unsqueeze(0) if relation_loss is not None else None),
            ("logits", prediction_scores.argmax(2)),
            # ("noise_detect_logits", noise_detect_scores.argmax(-1) if noise_detect_scores is not None and len(noise_detect_scores) > 0 else None),
        ])


class RoBertaForWikiKGPLM(RobertaPreTrainedModel):

    def __init__(self, config):
        super().__init__(config)
        self.num_labels = config.num_labels
        self.config = config
        self.roberta = RobertaModel(config)
        classifier_dropout = (
            config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
        )
        self.dropout = nn.Dropout(classifier_dropout)
        # self.cls = BertOnlyMLMHead(config)
        self.lm_head = RobertaLMHead(config) # Masked Language Modeling head
        self.detector = NSPHead(config) # Knowledge Noise Detection head
        self.entity_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        self.relation_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        # self.classifiers = nn.ModuleList([nn.Linear(config.hidden_size, config.num_ner_labels) for _ in range(config.entity_type_num)])

        self.contrastive_loss_fn = ContrastiveLoss()
        self.post_init()

        self.tokenizer = RobertaTokenizer.from_pretrained(config.name_or_path)

    def forward(
            self,
            input_ids=None,
            attention_mask=None,
            token_type_ids=None,
            position_ids=None,
            head_mask=None,
            inputs_embeds=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            mlm_labels=None,
            entity_label=None,
            entity_negative=None,
            relation_label=None,
            relation_negative=None,
            noise_detect_label=None,
            task_id=None,
            mask_id=None,
            output_attentions=None,
            output_hidden_states=None,
            return_dict=None,
    ):
        # start_time = time()

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        # print("attention_mask.shape=", attention_mask.shape)
        # print("input_ids[0]=", input_ids[0])
        # print("token_type_ids[0]=", token_type_ids[0])
        # attention_mask = None


        outputs = self.roberta(
            input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            encoder_hidden_states=encoder_hidden_states,
            encoder_attention_mask=encoder_attention_mask,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output, pooled_output = outputs[:2]
        prediction_scores = self.lm_head(sequence_output) # mlm head
        noise_detect_scores = self.detector(pooled_output) # knowledge noise detector


        # ner
        # sequence_output = self.dropout(sequence_output)
        # ner_logits = torch.stack([classifier(sequence_output) for classifier in self.classifiers]).movedim(1, 0)

        # mlm
        masked_lm_loss, noise_detect_loss, entity_loss, total_loss = None, None, None, None
        if mlm_labels is not None:
            loss_fct = CrossEntropyLoss()  # -100 index = padding token
            masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), mlm_labels.view(-1))

        if noise_detect_label is not None:
            loss_fct = CrossEntropyLoss()
            noise_detect_loss = loss_fct(noise_detect_scores.view(-1, 2), noise_detect_label.view(-1))
            total_loss = masked_lm_loss + noise_detect_loss

        if entity_label is not None and entity_negative is not None:
            batch_size = input_ids.shape[0]
            negative_num = entity_negative.shape[1]
            # print("negative_num=", negative_num)
            # 获取被mask实体的embedding
            batch_query_embedding = list()
            for ei, input_id in enumerate(input_ids):
                batch_query_embedding.append(torch.mean(sequence_output[ei][input_id == mask_id[ei]], 0)) # [hidden_dim]
            batch_query_embedding = torch.stack(batch_query_embedding) # [bz, dim]
            # print("batch_query_embedding.shape=", batch_query_embedding.shape)
            batch_query_embedding = self.entity_mlp(batch_query_embedding) # [bz, dim]
            batch_query_embedding = batch_query_embedding.unsqueeze(1).repeat((1, negative_num + 1, 1)) # [bz, 11, dim]
            batch_query_embedding = batch_query_embedding.view(-1, batch_query_embedding.shape[-1]) # [bz * 11, dim]
            # print("batch_query_embedding.shape=", batch_query_embedding.shape)

            # 获得positive和negative的BERT表示
            # entity_label: [bz, len], entity_negative: [bz, 10, len]
            entity_negative = entity_negative.view(-1, entity_negative.shape[-1]) # [bz * 10, len]
            entity_label_embedding = self.roberta.embeddings(input_ids=entity_label) # [bz, len, dim]
            entity_label_embedding = self.entity_mlp(torch.mean(entity_label_embedding, 1)) # [bz, dim]
            entity_label_embedding = entity_label_embedding.unsqueeze(1) # [bz, 1, dim]

            entity_negative_embedding = self.roberta.embeddings(input_ids=entity_negative) # [bz * 10, len, dim]
            entity_negative_embedding = self.entity_mlp(torch.mean(entity_negative_embedding, 1)) # [bz * 10, dim]
            entity_negative_embedding = entity_negative_embedding \
                .view(input_ids.shape[0], -1, entity_negative_embedding.shape[-1]) # [bz, 10, dim]

            contrastive_label = torch.Tensor([0] * negative_num + [1]).float().cuda()
            contrastive_label = contrastive_label.unsqueeze(0).repeat([batch_size, 1]).view(-1) # [bz * 11]
            # print("entity_negative_embedding.shape=", entity_negative_embedding.shape)
            # print("entity_label_embedding.shape=", entity_label_embedding.shape)
            candidate_embedding = torch.cat([entity_negative_embedding, entity_label_embedding], 1) # [bz, 11, dim]
            candidate_embedding = candidate_embedding.view(-1, candidate_embedding.shape[-1]) # [bz * 11, dim]
            # print("candidate_embedding.shape=", candidate_embedding.shape)

            entity_loss = self.contrastive_loss_fn(batch_query_embedding, candidate_embedding, contrastive_label)
            total_loss = masked_lm_loss + entity_loss


        # if ner_labels is not None:
        #     loss_fct = CrossEntropyLoss()
        #     # Only keep active parts of the loss
        #
        #     active_loss = attention_mask.repeat(self.config.entity_type_num, 1, 1).view(-1) == 1
        #     active_logits = ner_logits.reshape(-1, self.config.num_ner_labels)
        #     active_labels = torch.where(
        #         active_loss, ner_labels.view(-1), torch.tensor(loss_fct.ignore_index).type_as(ner_labels)
        #     )
        #     ner_loss = loss_fct(active_logits, active_labels)
        #
        # if masked_lm_loss:
        #     total_loss = masked_lm_loss + ner_loss * 4
        # print("total_loss=", total_loss)
        # print("mlm_loss=", masked_lm_loss)


        # end_time = time()
        # print("neural_mode_time: {}".format(end_time - start_time))

        return OrderedDict([
            ("loss", total_loss),
            ("mlm_loss", masked_lm_loss.unsqueeze(0)),
            ("noise_detect_loss", noise_detect_loss.unsqueeze(0) if noise_detect_loss is not None else None),
            ("entity_loss", entity_loss.unsqueeze(0) if entity_label is not None else None),
            ("logits", prediction_scores.argmax(2)),
            ("noise_detect_logits", noise_detect_scores.argmax(-1) if noise_detect_scores is not None else None),
        ])
        # MaskedLMOutput(
        #     loss=total_loss,
        #     logits=prediction_scores.argmax(2),
        #     ner_l
        #     hidden_states=outputs.hidden_states,
        #     attentions=outputs.attentions,
        # )




class BertForWikiKGPLM(BertPreTrainedModel):

    def __init__(self, config):
        super().__init__(config)
        self.num_labels = config.num_labels
        self.config = config
        self.bert = BertModel(config)
        classifier_dropout = (
            config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
        )
        self.dropout = nn.Dropout(classifier_dropout)
        # self.cls = BertOnlyMLMHead(config)
        self.cls = BertPreTrainedModel(config)
        self.entity_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        self.relation_mlp = nn.Linear(config.hidden_size, config.hidden_size)
        # self.classifiers = nn.ModuleList([nn.Linear(config.hidden_size, config.num_ner_labels) for _ in range(config.entity_type_num)])

        self.contrastive_loss_fn = ContrastiveLoss()
        self.post_init()

    def forward(
            self,
            input_ids=None,
            attention_mask=None,
            token_type_ids=None,
            position_ids=None,
            head_mask=None,
            inputs_embeds=None,
            encoder_hidden_states=None,
            encoder_attention_mask=None,
            mlm_labels=None,
            entity_label=None,
            entity_negative=None,
            relation_label=None,
            relation_negative=None,
            noise_detect_label=None,
            task_id=None,
            mask_id=None,
            output_attentions=None,
            output_hidden_states=None,
            return_dict=None,
    ):

        return_dict = return_dict if return_dict is not None else self.config.use_return_dict
        print("attention_mask.shape=", attention_mask.shape)
        print("input_ids[0]=", input_ids[0])
        print("token_type_ids[0]=", token_type_ids[0])
        attention_mask = None
        outputs = self.bert(
            input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            encoder_hidden_states=encoder_hidden_states,
            encoder_attention_mask=encoder_attention_mask,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )

        sequence_output, pooled_output = outputs[:2]
        prediction_scores, seq_relationship_score = self.cls(sequence_output, pooled_output)

        # ner
        # sequence_output = self.dropout(sequence_output)
        # ner_logits = torch.stack([classifier(sequence_output) for classifier in self.classifiers]).movedim(1, 0)

        # mlm
        masked_lm_loss, noise_detect_loss, entity_loss, total_loss = None, None, None, None

        if mlm_labels is not None:
            loss_fct = CrossEntropyLoss()  # -100 index = padding token
            masked_lm_loss = loss_fct(prediction_scores.view(-1, self.config.vocab_size), mlm_labels.view(-1))

        if noise_detect_label is not None:
            loss_fct = CrossEntropyLoss()
            noise_detect_loss = loss_fct(seq_relationship_score.view(-1, 2), noise_detect_label.view(-1))
            total_loss = masked_lm_loss + noise_detect_loss

        if entity_label is not None and entity_negative is not None:
            negative_num = entity_negative.shape[1]
            # 获取被mask实体的embedding
            batch_query_embedding = list()
            for ei, input_id in enumerate(input_ids):
                batch_query_embedding.append(torch.mean(sequence_output[ei][input_id == mask_id[ei]], 0)) # [hidden_dim]
            batch_query_embedding = torch.stack(batch_query_embedding) # [bz, dim]
            batch_query_embedding = self.entity_mlp(batch_query_embedding) # [bz, dim]
            batch_query_embedding = batch_query_embedding.repeat((1, negative_num + 1, 1)) # [bz, 11, dim]

            # 获得positive和negative的BERT表示
            # entity_label: [bz, len], entity_negative: [bz, 10, len]
            entity_negative = entity_negative.view(-1, entity_negative.shape[-1]) # [bz * 10, len]
            entity_label_embedding = self.bert.embeddings(input_id=entity_label) # [bz, len, dim]
            entity_label_embedding = self.entity_mlp(torch.mean(entity_label_embedding, 1)) # [bz, dim]
            entity_label_embedding = entity_label_embedding.unsqueeze(1) # [bz, 1, dim]

            entity_negative_embedding = self.bert.embeddings(input_id=entity_negative) # [bz * 10, len, dim]
            entity_negative_embedding = self.entity_mlp(torch.mean(entity_negative_embedding, 1)) # [bz * 10, dim]
            entity_negative_embedding = entity_negative_embedding \
                .view(input_ids.shape[0], -1, entity_negative_embedding.shape[-1]) # [bz, 10, dim]

            contrastive_label = torch.Tensor([0] * negative_num + [1]).float().cuda()
            candidate_embedding = torch.cat([entity_negative_embedding, entity_label_embedding], 1) # [bz, 11, dim]

            entity_loss = self.contrastive_loss_fn(batch_query_embedding, candidate_embedding, contrastive_label)
            total_loss = masked_lm_loss + entity_loss


        # if ner_labels is not None:
        #     loss_fct = CrossEntropyLoss()
        #     # Only keep active parts of the loss
        #
        #     active_loss = attention_mask.repeat(self.config.entity_type_num, 1, 1).view(-1) == 1
        #     active_logits = ner_logits.reshape(-1, self.config.num_ner_labels)
        #     active_labels = torch.where(
        #         active_loss, ner_labels.view(-1), torch.tensor(loss_fct.ignore_index).type_as(ner_labels)
        #     )
        #     ner_loss = loss_fct(active_logits, active_labels)
        #
        # if masked_lm_loss:
        #     total_loss = masked_lm_loss + ner_loss * 4

        return OrderedDict([
            ("loss", total_loss),
            ("mlm_loss", masked_lm_loss.unsqueeze(0)),
            ("noise_detect_loss", noise_detect_loss.unsqueeze(0)),
            ("entity_loss", entity_loss.unsqueeze(0)),
            ("logits", prediction_scores.argmax(2)),
            ("noise_detect_logits", seq_relationship_score.argmax(3)),
            ()
        ])
        # MaskedLMOutput(
        #     loss=total_loss,
        #     logits=prediction_scores.argmax(2),
        #     ner_l
        #     hidden_states=outputs.hidden_states,
        #     attentions=outputs.attentions,
        # )