| 81 | |
| 82 | |
| 83 | class ConstraintSeq2SeqTrainer(Seq2SeqTrainer): |
| 84 | def __init__(self, decoding_type_schema=None, decoding_format='tree', source_prefix=None, *args, **kwargs): |
| 85 | super().__init__(*args, **kwargs) |
| 86 | |
| 87 | self.decoding_format = decoding_format |
| 88 | self.decoding_type_schema = decoding_type_schema # 在event任务中为 event schema |
| 89 | |
| 90 | # Label smoothing by sum token loss, different from different Label smootheing |
| 91 | if self.args.label_smoothing_sum and self.args.label_smoothing_factor != 0: |
| 92 | self.label_smoother = SumLabelSmoother(epsilon=self.args.label_smoothing_factor) |
| 93 | print('Using %s' % self.label_smoother) |
| 94 | elif self.args.label_smoothing_factor != 0: |
| 95 | print('Using %s' % self.label_smoother) |
| 96 | else: |
| 97 | self.label_smoother = None |
| 98 | |
| 99 | if self.args.constraint_decoding: |
| 100 | self.constraint_decoder = get_constraint_decoder(tokenizer=self.tokenizer, |
| 101 | type_schema=self.decoding_type_schema, |
| 102 | decoding_schema=self.decoding_format, |
| 103 | source_prefix=source_prefix) |
| 104 | else: |
| 105 | self.constraint_decoder = None |
| 106 | |
| 107 | def prediction_step( |
| 108 | self, |
| 109 | model: nn.Module, |
| 110 | inputs: Dict[str, Union[torch.Tensor, Any]], |
| 111 | prediction_loss_only: bool, |
| 112 | ignore_keys: Optional[List[str]] = None, |
| 113 | ) -> Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: |
| 114 | """ |
| 115 | Perform an evaluation step on :obj:`model` using obj:`inputs`. |
| 116 | |
| 117 | Subclass and override to inject custom behavior. |
| 118 | |
| 119 | Args: |
| 120 | model (:obj:`nn.Module`): |
| 121 | The model to evaluate. |
| 122 | inputs (:obj:`Dict[str, Union[torch.Tensor, Any]]`): |
| 123 | The inputs and targets of the model. |
| 124 | |
| 125 | The dictionary will be unpacked before being fed to the model. Most models expect the targets under the |
| 126 | argument :obj:`labels`. Check your model's documentation for all accepted arguments. |
| 127 | prediction_loss_only (:obj:`bool`): |
| 128 | Whether or not to return the loss only. |
| 129 | |
| 130 | Return: |
| 131 | Tuple[Optional[float], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, logits and |
| 132 | labels (each being optional). |
| 133 | """ |
| 134 | |
| 135 | def prefix_allowed_tokens_fn(batch_id, sent): |
| 136 | # print(self.tokenizer.convert_ids_to_tokens(inputs['labels'][batch_id])) |
| 137 | src_sentence = inputs['input_ids'][batch_id] |
| 138 | # print("input_ids:", inputs.keys()) |
| 139 | # print("src_sentece:", src_sentence) |
| 140 | # print("sent:", sent) |