MCPcopy Create free account
hub / github.com/Hzfinfdu/Diffusion-BERT / forward

Method forward

models/modeling_bert.py:1641–1705  ·  view source on GitHub ↗

r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the multiple choice classification loss. Indices should be in `[0, ..., num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See

(
        self,
        input_ids: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        token_type_ids: Optional[torch.Tensor] = None,
        position_ids: Optional[torch.Tensor] = None,
        head_mask: Optional[torch.Tensor] = None,
        inputs_embeds: Optional[torch.Tensor] = None,
        labels: Optional[torch.Tensor] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
    )

Source from the content-addressed store, hash-verified

1639 config_class=_CONFIG_FOR_DOC,
1640 )
1641 def forward(
1642 self,
1643 input_ids: Optional[torch.Tensor] = None,
1644 attention_mask: Optional[torch.Tensor] = None,
1645 token_type_ids: Optional[torch.Tensor] = None,
1646 position_ids: Optional[torch.Tensor] = None,
1647 head_mask: Optional[torch.Tensor] = None,
1648 inputs_embeds: Optional[torch.Tensor] = None,
1649 labels: Optional[torch.Tensor] = None,
1650 output_attentions: Optional[bool] = None,
1651 output_hidden_states: Optional[bool] = None,
1652 return_dict: Optional[bool] = None,
1653 ) -> Union[Tuple[torch.Tensor], MultipleChoiceModelOutput]:
1654 r"""
1655 labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
1656 Labels for computing the multiple choice classification loss. Indices should be in `[0, ...,
1657 num_choices-1]` where `num_choices` is the size of the second dimension of the input tensors. (See
1658 `input_ids` above)
1659 """
1660 return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1661 num_choices = input_ids.shape[1] if input_ids is not None else inputs_embeds.shape[1]
1662
1663 input_ids = input_ids.view(-1, input_ids.size(-1)) if input_ids is not None else None
1664 attention_mask = attention_mask.view(-1, attention_mask.size(-1)) if attention_mask is not None else None
1665 token_type_ids = token_type_ids.view(-1, token_type_ids.size(-1)) if token_type_ids is not None else None
1666 position_ids = position_ids.view(-1, position_ids.size(-1)) if position_ids is not None else None
1667 inputs_embeds = (
1668 inputs_embeds.view(-1, inputs_embeds.size(-2), inputs_embeds.size(-1))
1669 if inputs_embeds is not None
1670 else None
1671 )
1672
1673 outputs = self.bert(
1674 input_ids,
1675 attention_mask=attention_mask,
1676 token_type_ids=token_type_ids,
1677 position_ids=position_ids,
1678 head_mask=head_mask,
1679 inputs_embeds=inputs_embeds,
1680 output_attentions=output_attentions,
1681 output_hidden_states=output_hidden_states,
1682 return_dict=return_dict,
1683 )
1684
1685 pooled_output = outputs[1]
1686
1687 pooled_output = self.dropout(pooled_output)
1688 logits = self.classifier(pooled_output)
1689 reshaped_logits = logits.view(-1, num_choices)
1690
1691 loss = None
1692 if labels is not None:
1693 loss_fct = CrossEntropyLoss()
1694 loss = loss_fct(reshaped_logits, labels)
1695
1696 if not return_dict:
1697 output = (reshaped_logits,) + outputs[2:]
1698 return ((loss,) + output) if loss is not None else output

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected