output: tuple: (loss, ) in training
| 9 | WEIGHTS_NAME = "pytorch_model.bin" |
| 10 | |
| 11 | class BARTParser(nn.Module): |
| 12 | """ |
| 13 | output: tuple: (loss, ) in training |
| 14 | """ |
| 15 | def __init__(self): |
| 16 | super().__init__() |
| 17 | self.bert = BartForTextToSQL.from_pretrained("facebook/bart-large") |
| 18 | |
| 19 | |
| 20 | def forward(self, *input, **kwargs): |
| 21 | input_token_ids = kwargs.pop("input_ids") |
| 22 | column_spans = kwargs.pop("column_spans") |
| 23 | input_padding_id = kwargs.pop("input_padding_id") |
| 24 | # copy_span = kwargs.pop("copy_span", None) |
| 25 | copy_span = None |
| 26 | attention_mask = (input_token_ids != input_padding_id).long() |
| 27 | # relation_ids = None |
| 28 | if self.training: |
| 29 | label_ids = kwargs.pop("labels") |
| 30 | label_padding_id = kwargs.pop("label_padding_id") |
| 31 | # encoded = self.bert.encoder(input_token_ids)[0].contiguous() |
| 32 | y_ids = label_ids[:, :-1].contiguous() |
| 33 | lm_labels = label_ids[:, 1:].clone() |
| 34 | lm_labels[label_ids[:, 1:] == label_padding_id] = -100 |
| 35 | outputs = self.bert(input_token_ids, column_spans=column_spans, copy_span=copy_span, |
| 36 | attention_mask=attention_mask, decoder_input_ids=y_ids, lm_labels=lm_labels, ) |
| 37 | return (outputs[0],) |
| 38 | |
| 39 | else: |
| 40 | label_eos_id = kwargs.pop("label_eos_id") |
| 41 | label_bos_id = kwargs.pop("label_bos_id") |
| 42 | label_padding_id = kwargs.pop("label_padding_id") |
| 43 | generated_ids = self.bert.generate( |
| 44 | input_ids=input_token_ids, |
| 45 | column_spans=column_spans, |
| 46 | copy_span=copy_span, |
| 47 | attention_mask=attention_mask, |
| 48 | num_beams=1, |
| 49 | max_length=30, |
| 50 | length_penalty=2.0, |
| 51 | early_stopping=True, |
| 52 | use_cache=True, |
| 53 | decoder_start_token_id=label_bos_id, |
| 54 | eos_token_id=label_eos_id, |
| 55 | pad_token_id=label_padding_id, |
| 56 | vocab_size=len(KEYWORDS) |
| 57 | ) |
| 58 | # label_ids = kwargs.pop("label_ids") |
| 59 | # label_padding_id = kwargs.pop("label_padding_id") |
| 60 | # # encoded = self.bert.encoder(input_token_ids)[0].contiguous() |
| 61 | # y_ids = label_ids[:, :-1].contiguous() |
| 62 | # lm_labels = label_ids[:, 1:].clone() |
| 63 | # lm_labels[label_ids[:, 1:] == label_padding_id] = -100 |
| 64 | # outputs = self.bert(input_token_ids, column_spans=column_spans, |
| 65 | # attention_mask=attention_mask, decoder_input_ids=y_ids, lm_labels=lm_labels, ) |
| 66 | # generated_ids = outputs[-1] |
| 67 | # raise NotImplementedError() |
| 68 | return (torch.zeros(1), generated_ids) |
nothing calls this directly
no outgoing calls
no test coverage detected