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
| def cl_forward(cls, encoder, input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None, mlm_input_ids=None, mlm_labels=None, ): return_dict = return_dict if return_dict is not None else cls.config.use_return_dict ori_input_ids = input_ids batch_size = input_ids.size(0) num_sent = input_ids.size(1)
mlm_outputs = None input_ids = input_ids.view((-1, input_ids.size(-1))) attention_mask = attention_mask.view((-1, attention_mask.size(-1))) if token_type_ids is not None: token_type_ids = token_type_ids.view((-1, token_type_ids.size(-1)))
outputs = encoder( input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, output_attentions=output_attentions, output_hidden_states=True if cls.model_args.pooler_type in ['avg_top2', 'avg_first_last'] else False, return_dict=True, )
if mlm_input_ids is not None: mlm_input_ids = mlm_input_ids.view((-1, mlm_input_ids.size(-1))) mlm_outputs = encoder( mlm_input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds, output_attentions=output_attentions, output_hidden_states=True if cls.model_args.pooler_type in ['avg_top2', 'avg_first_last'] else False, return_dict=True, )
pooler_output = cls.pooler(attention_mask, outputs) pooler_output = pooler_output.view((batch_size, num_sent, pooler_output.size(-1)))
if cls.pooler_type == "cls": pooler_output = cls.mlp(pooler_output)
z1, z2 = pooler_output[:,0], pooler_output[:,1]
if num_sent == 3: z3 = pooler_output[:, 2]
if dist.is_initialized() and cls.training: if num_sent >= 3: z3_list = [torch.zeros_like(z3) for _ in range(dist.get_world_size())] dist.all_gather(tensor_list=z3_list, tensor=z3.contiguous()) z3_list[dist.get_rank()] = z3 z3 = torch.cat(z3_list, 0)
z1_list = [torch.zeros_like(z1) for _ in range(dist.get_world_size())] z2_list = [torch.zeros_like(z2) for _ in range(dist.get_world_size())] dist.all_gather(tensor_list=z1_list, tensor=z1.contiguous()) dist.all_gather(tensor_list=z2_list, tensor=z2.contiguous())
z1_list[dist.get_rank()] = z1 z2_list[dist.get_rank()] = z2 z1 = torch.cat(z1_list, 0) z2 = torch.cat(z2_list, 0)
cos_sim = cls.sim(z1.unsqueeze(1), z2.unsqueeze(0)) if num_sent >= 3: z1_z3_cos = cls.sim(z1.unsqueeze(1), z3.unsqueeze(0)) cos_sim = torch.cat([cos_sim, z1_z3_cos], 1)
labels = torch.arange(cos_sim.size(0)).long().to(cls.device) loss_fct = nn.CrossEntropyLoss()
if num_sent == 3: z3_weight = cls.model_args.hard_negative_weight weights = torch.tensor( [[0.0] * (cos_sim.size(-1) - z1_z3_cos.size(-1)) + [0.0] * i + [z3_weight] + [0.0] * (z1_z3_cos.size(-1) - i - 1) for i in range(z1_z3_cos.size(-1))] ).to(cls.device) cos_sim = cos_sim + weights
loss = loss_fct(cos_sim, labels)
if mlm_outputs is not None and mlm_labels is not None: mlm_labels = mlm_labels.view(-1, mlm_labels.size(-1)) prediction_scores = cls.lm_head(mlm_outputs.last_hidden_state) masked_lm_loss = loss_fct(prediction_scores.view(-1, cls.config.vocab_size), mlm_labels.view(-1)) loss = loss + cls.model_args.mlm_weight * masked_lm_loss
if not return_dict: output = (cos_sim,) + outputs[2:] return ((loss,) + output) if loss is not None else output return SequenceClassifierOutput( loss=loss, logits=cos_sim, hidden_states=outputs.hidden_states, attentions=outputs.attentions, )
|