r""" labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]
(
self,
input_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
attention_mask: Optional[torch.FloatTensor] = None,
token_type_ids: Optional[torch.LongTensor] = None,
position_ids: Optional[torch.LongTensor] = None,
head_mask: Optional[torch.FloatTensor] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
)
| 653 | config_class=_CONFIG_FOR_DOC, |
| 654 | ) |
| 655 | def forward( |
| 656 | self, |
| 657 | input_ids: Optional[torch.LongTensor] = None, |
| 658 | past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None, |
| 659 | attention_mask: Optional[torch.FloatTensor] = None, |
| 660 | token_type_ids: Optional[torch.LongTensor] = None, |
| 661 | position_ids: Optional[torch.LongTensor] = None, |
| 662 | head_mask: Optional[torch.FloatTensor] = None, |
| 663 | inputs_embeds: Optional[torch.FloatTensor] = None, |
| 664 | labels: Optional[torch.LongTensor] = None, |
| 665 | use_cache: Optional[bool] = None, |
| 666 | output_attentions: Optional[bool] = None, |
| 667 | output_hidden_states: Optional[bool] = None, |
| 668 | return_dict: Optional[bool] = None, |
| 669 | ) -> Union[Tuple, CausalLMOutputWithPast]: |
| 670 | r""" |
| 671 | labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): |
| 672 | Labels for language modeling. Note that the labels **are shifted** inside the model, i.e. you can set |
| 673 | `labels = input_ids` Indices are selected in `[-100, 0, ..., config.vocab_size]` All labels set to `-100` |
| 674 | are ignored (masked), the loss is only computed for labels in `[0, ..., config.vocab_size]` |
| 675 | """ |
| 676 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 677 | |
| 678 | transformer_outputs = self.transformer( |
| 679 | input_ids, |
| 680 | past_key_values=past_key_values, |
| 681 | attention_mask=attention_mask, |
| 682 | token_type_ids=token_type_ids, |
| 683 | position_ids=position_ids, |
| 684 | head_mask=head_mask, |
| 685 | inputs_embeds=inputs_embeds, |
| 686 | use_cache=use_cache, |
| 687 | output_attentions=output_attentions, |
| 688 | output_hidden_states=output_hidden_states, |
| 689 | return_dict=return_dict, |
| 690 | ) |
| 691 | hidden_states = transformer_outputs[0] |
| 692 | |
| 693 | # make sure sampling in fp16 works correctly and |
| 694 | # compute loss in fp32 to match with mesh-tf version |
| 695 | # https://github.com/EleutherAI/gpt-neo/blob/89ce74164da2fb16179106f54e2269b5da8db333/models/gpt2/gpt2.py#L179 |
| 696 | lm_logits = self.lm_head(hidden_states).to(torch.float32) |
| 697 | |
| 698 | loss = None |
| 699 | if labels is not None: |
| 700 | # Shift so that tokens < n predict n |
| 701 | shift_logits = lm_logits[..., :-1, :].contiguous() |
| 702 | shift_labels = labels[..., 1:].contiguous() |
| 703 | # Flatten the tokens |
| 704 | loss_fct = CrossEntropyLoss() |
| 705 | loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) |
| 706 | |
| 707 | loss = loss.to(hidden_states.dtype) |
| 708 | |
| 709 | if not return_dict: |
| 710 | output = (lm_logits,) + transformer_outputs[1:] |
| 711 | return ((loss,) + output) if loss is not None else output |
| 712 |
nothing calls this directly
no outgoing calls
no test coverage detected