(self, batch, batch_idx)
| 39 | fishmask_plugin_on_init(self) |
| 40 | |
| 41 | def training_step(self, batch, batch_idx): |
| 42 | if self.config.model_modifier == "intrinsic": |
| 43 | from .intrinsic import intrinsic_plugin_on_step |
| 44 | intrinsic_plugin_on_step(self) |
| 45 | |
| 46 | if self.config.mc_loss > 0 or self.config.unlikely_loss > 0: |
| 47 | input_ids, choices_ids, labels = batch["input_ids"], batch["answer_choices_ids"], batch["labels"] |
| 48 | bs, num_choices = choices_ids.size()[:2] |
| 49 | |
| 50 | flat_choices_ids = choices_ids.flatten(0, 1) |
| 51 | attention_mask = (input_ids != self.tokenizer.pad_token_id).float() # [bs, max_seq_len] |
| 52 | encoder_hidden_states = self.model.encoder(input_ids=input_ids, attention_mask=attention_mask)[0] |
| 53 | encoder_hidden_states = encoder_hidden_states.unsqueeze(dim=1).repeat(1, num_choices, 1, 1).flatten(0, 1) |
| 54 | attention_mask = attention_mask.unsqueeze(dim=1).repeat(1, num_choices, 1).flatten(0, 1) |
| 55 | decoder_input_ids = torch.cat([torch.zeros_like(flat_choices_ids[:, :1]), flat_choices_ids[:, :-1]], dim=1) |
| 56 | decoder_attention_mask = (decoder_input_ids == decoder_input_ids).float() |
| 57 | lm_target = flat_choices_ids - 100 * (flat_choices_ids == self.tokenizer.pad_token_id).long() |
| 58 | |
| 59 | model_output = self.model( |
| 60 | attention_mask=attention_mask, |
| 61 | encoder_outputs=[encoder_hidden_states], |
| 62 | decoder_input_ids=decoder_input_ids, |
| 63 | decoder_attention_mask=decoder_attention_mask, |
| 64 | ) |
| 65 | choices_scores = ( |
| 66 | F.cross_entropy(model_output.logits.flatten(0, 1), lm_target.flatten(0, 1), reduction="none") |
| 67 | .view(bs, num_choices, -1) |
| 68 | .sum(dim=-1) |
| 69 | ) |
| 70 | # Length normalization |
| 71 | if self.config.length_norm > 0: |
| 72 | choices_scores = choices_scores / torch.pow( |
| 73 | (choices_ids != self.tokenizer.pad_token_id).sum(dim=-1), self.config.length_norm |
| 74 | ) |
| 75 | lm_loss = F.cross_entropy( |
| 76 | model_output.logits.view(bs, num_choices, *model_output.logits.size()[1:])[range(bs), labels].flatten( |
| 77 | 0, 1 |
| 78 | ), |
| 79 | lm_target.view(bs, num_choices, -1)[range(bs), labels].flatten(0, 1), |
| 80 | ) |
| 81 | |
| 82 | tensorboard_logs = {"lm_loss": lm_loss.item()} |
| 83 | # I think mc loss corresponds to the LN-loss which is a softmax-cross entropy loss for length normalized |
| 84 | # output sequences |
| 85 | if self.config.mc_loss > 0: |
| 86 | mc_loss = F.cross_entropy(-choices_scores, labels) |
| 87 | tensorboard_logs["mc_loss"] = mc_loss.item() |
| 88 | else: |
| 89 | mc_loss = 0.0 |
| 90 | |
| 91 | if self.config.unlikely_loss > 0: |
| 92 | cand_loglikely = -F.cross_entropy( |
| 93 | model_output.logits.flatten(0, 1), lm_target.flatten(0, 1), reduction="none" |
| 94 | ).view(bs, num_choices, -1) |
| 95 | cand_loglikely += (lm_target < 0).view(bs, num_choices, -1) * -100 |
| 96 | cand_loglikely[range(bs), labels] = -100 |
| 97 | unlikely_loss = -torch.log(1 - torch.exp(cand_loglikely) + 1e-2).sum() / (cand_loglikely != -100).sum() |
| 98 | tensorboard_logs["unlikely_loss"] = unlikely_loss.item() |
nothing calls this directly
no test coverage detected