src/transformers/models/luke/modeling_luke.py/LukeForEntitySpanClassification/forward
class LukeForEntitySpanClassification:    def forward(
        self,
        input_ids=None,
        attention_mask=None,
        token_type_ids=None,
        position_ids=None,
        entity_ids=None,
        entity_attention_mask=None,
        entity_token_type_ids=None,
        entity_position_ids=None,
        entity_start_positions=None,
        entity_end_positions=None,
        head_mask=None,
        inputs_embeds=None,
        labels=None,
        output_attentions=None,
        output_hidden_states=None,
        return_dict=None,
    ):
        r"""
        entity_start_positions (:obj:`torch.LongTensor`):
            The start positions of entities in the word token sequence.

        entity_end_positions (:obj:`torch.LongTensor`):
            The end positions of entities in the word token sequence.

        labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size, entity_length)` or :obj:`(batch_size, entity_length, num_labels)`, `optional`):
            Labels for computing the classification loss. If the shape is :obj:`(batch_size, entity_length)`, the cross
            entropy loss is used for the single-label classification. In this case, labels should contain the indices
            that should be in :obj:`[0, ..., config.num_labels - 1]`. If the shape is :obj:`(batch_size, entity_length,
            num_labels)`, the binary cross entropy loss is used for the multi-label classification. In this case,
            labels should only contain ``[0, 1]``, where 0 and 1 indicate false and true, respectively.

        Returns:

        Examples::

            >>> from transformers import LukeTokenizer, LukeForEntitySpanClassification

            >>> tokenizer = LukeTokenizer.from_pretrained("studio-ousia/luke-large-finetuned-conll-2003")
            >>> model = LukeForEntitySpanClassification.from_pretrained("studio-ousia/luke-large-finetuned-conll-2003")

            >>> text = "Beyoncé lives in Los Angeles"

            # List all possible entity spans in the text
            >>> word_start_positions = [0, 8, 14, 17, 21]  # character-based start positions of word tokens
            >>> word_end_positions = [7, 13, 16, 20, 28]  # character-based end positions of word tokens
            >>> entity_spans = []
            >>> for i, start_pos in enumerate(word_start_positions):
            ...     for end_pos in word_end_positions[i:]:
            ...         entity_spans.append((start_pos, end_pos))

            >>> inputs = tokenizer(text, entity_spans=entity_spans, return_tensors="pt")
            >>> outputs = model(**inputs)
            >>> logits = outputs.logits
            >>> predicted_class_indices = logits.argmax(-1).squeeze().tolist()
            >>> for span, predicted_class_idx in zip(entity_spans, predicted_class_indices):
            ...     if predicted_class_idx != 0:
            ...        print(text[span[0]:span[1]], model.config.id2label[predicted_class_idx])
            Beyoncé PER
            Los Angeles LOC
        """
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        outputs = self.luke(
            input_ids=input_ids,
            attention_mask=attention_mask,
            token_type_ids=token_type_ids,
            position_ids=position_ids,
            entity_ids=entity_ids,
            entity_attention_mask=entity_attention_mask,
            entity_token_type_ids=entity_token_type_ids,
            entity_position_ids=entity_position_ids,
            head_mask=head_mask,
            inputs_embeds=inputs_embeds,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=True,
        )
        hidden_size = outputs.last_hidden_state.size(-1)

        entity_start_positions = entity_start_positions.unsqueeze(-1).expand(-1, -1, hidden_size)
        start_states = torch.gather(outputs.last_hidden_state, -2, entity_start_positions)
        entity_end_positions = entity_end_positions.unsqueeze(-1).expand(-1, -1, hidden_size)
        end_states = torch.gather(outputs.last_hidden_state, -2, entity_end_positions)
        feature_vector = torch.cat([start_states, end_states, outputs.entity_last_hidden_state], dim=2)

        feature_vector = self.dropout(feature_vector)
        logits = self.classifier(feature_vector)

        loss = None
        if labels is not None:
            # When the number of dimension of `labels` is 2, cross entropy is used as the loss function. The binary
            # cross entropy is used otherwise.
            if labels.ndim == 2:
                loss = nn.functional.cross_entropy(logits.view(-1, self.num_labels), labels.view(-1))
            else:
                loss = nn.functional.binary_cross_entropy_with_logits(logits.view(-1), labels.view(-1).type_as(logits))

        if not return_dict:
            output = (
                logits,
                outputs.hidden_states,
                outputs.entity_hidden_states,
                outputs.attentions,
            )
            return ((loss,) + output) if loss is not None else output

        return EntitySpanClassificationOutput(
            loss=loss,
            logits=logits,
            hidden_states=outputs.hidden_states,
            entity_hidden_states=outputs.entity_hidden_states,
            attentions=outputs.attentions,
        )