| 19 | BART_START_DOCSTRING + BART_GENERATION_EXAMPLE, |
| 20 | ) |
| 21 | class BartForTextToSQL(PretrainedBartModel): |
| 22 | base_model_prefix = "model" |
| 23 | |
| 24 | def __init__(self, config: BartConfig): |
| 25 | super().__init__(config) |
| 26 | base_model = BartModel(config) |
| 27 | self.model = base_model |
| 28 | # self.register_buffer("final_logits_bias", torch.zeros((1, self.model.shared.num_embeddings))) |
| 29 | # self.average_extractor = AverageSpanExtractor() |
| 30 | # self.linear = nn.Linear(in_features=config.d_model, out_features=config.d_model) |
| 31 | |
| 32 | |
| 33 | def resize_token_embeddings(self, new_num_tokens: int) -> nn.Embedding: |
| 34 | old_num_tokens = self.model.shared.num_embeddings |
| 35 | new_embeddings = super().resize_token_embeddings(new_num_tokens) |
| 36 | self.model.shared = new_embeddings |
| 37 | self._resize_final_logits_bias(new_num_tokens, old_num_tokens) |
| 38 | return new_embeddings |
| 39 | |
| 40 | def _resize_final_logits_bias(self, new_num_tokens: int, old_num_tokens: int) -> None: |
| 41 | if new_num_tokens <= old_num_tokens: |
| 42 | new_bias = self.final_logits_bias[:, :new_num_tokens] |
| 43 | else: |
| 44 | extra_bias = torch.zeros((1, new_num_tokens - old_num_tokens), device=self.final_logits_bias.device) |
| 45 | new_bias = torch.cat([self.final_logits_bias, extra_bias], dim=1) |
| 46 | self.register_buffer("final_logits_bias", new_bias) |
| 47 | |
| 48 | @add_start_docstrings_to_callable(BART_INPUTS_DOCSTRING) |
| 49 | def forward( |
| 50 | self, |
| 51 | input_ids, |
| 52 | column_spans, |
| 53 | copy_span=None, |
| 54 | attention_mask=None, |
| 55 | encoder_outputs=None, |
| 56 | decoder_input_ids=None, |
| 57 | decoder_attention_mask=None, |
| 58 | decoder_cached_states=None, |
| 59 | lm_labels=None, |
| 60 | use_cache=False, |
| 61 | **unused |
| 62 | ): |
| 63 | r""" |
| 64 | masked_lm_labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, sequence_length)`, `optional`, defaults to :obj:`None`): |
| 65 | Labels for computing the masked language modeling loss. |
| 66 | Indices should either be in ``[0, ..., config.vocab_size]`` or -100 (see ``input_ids`` docstring). |
| 67 | Tokens with indices set to ``-100`` are ignored (masked), the loss is only computed for the tokens |
| 68 | with labels |
| 69 | in ``[0, ..., config.vocab_size]``. |
| 70 | |
| 71 | Returns: |
| 72 | :obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.RobertaConfig`) and inputs: |
| 73 | masked_lm_loss (`optional`, returned when ``masked_lm_labels`` is provided) ``torch.FloatTensor`` of shape ``(1,)``: |
| 74 | Masked language modeling loss. |
| 75 | prediction_scores (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, config.vocab_size)`) |
| 76 | Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax). |
| 77 | hidden_states (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_hidden_states=True``): |
| 78 | Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) |
nothing calls this directly
no outgoing calls
no test coverage detected