code: def forward(
      self,
      input_ids=None,
      attention_mask=None,
      token_type_ids=None,
      position_ids=None,
      head_mask=None,
      inputs_embeds=None,
      labels=None,
      pad_token_label_id=None,
  ):
      logits, outputs = self._get_features(
          input_ids,
          attention_mask,
          token_type_ids,
          position_ids,
          head_mask,
          inputs_embeds,
      )
  
      outputs = (logits,) + outputs[
          2:
      ]  # add hidden states and attention if they are here
      if labels is not None:
          # loss_fct = nn.CrossEntropyLoss()
          pad_mask = labels != pad_token_label_id
  
          # Only keep active parts of the loss
          if attention_mask is not None:
              # active_loss = attention_mask.view(-1) == 1
              # active_logits = logits.view(-1, self.num_labels)[active_loss]
              # active_labels = labels.view(-1)[active_loss]
              loss_mask = (attention_mask == 1) & pad_mask
          else:
              # loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
              loss_mask = (torch.ones(logits.shape) == 1) & pad_mask
  
          crf_labels, crf_mask = to_crf_pad(
              labels, loss_mask, pad_token_label_id
          )
          crf_logits, _ = to_crf_pad(
              logits, loss_mask, pad_token_label_id
          )
  
          loss = self.crf.neg_log_likelihood(
              crf_logits, crf_mask, crf_labels
          )
          # removing mask stuff from the output path is done later in my_crf_ner but it should be kept away
          # when calculating loss
          best_path = self.crf(
              crf_logits, crf_mask
          )  # (torch.ones(logits.shape) == 1)
          best_path = unpad_crf(best_path, crf_mask, labels, pad_mask)
          outputs = (loss,) + outputs + (best_path,)
      else:
          # removing mask stuff from the output path is done later in my_crf_ner but it should be kept away
          # when calculating loss
          if attention_mask is not None:
              mask = attention_mask == 1  # & (labels!=-100))
          else:
              mask = torch.ones(logits.shape).bool()  # (labels!=-100)
          crf_logits, crf_mask = to_crf_pad(
              logits, mask, pad_token_label_id
          )
          crf_mask = crf_mask.sum(axis=2) == crf_mask.shape[2]
          best_path = self.crf(crf_logits, crf_mask)
          temp_labels = torch.ones(mask.shape) * pad_token_label_id
          best_path = unpad_crf(best_path, crf_mask, temp_labels, mask)
          outputs = outputs + (best_path,)
  
      return outputs
  
file_path: DeepKE/src/deepke/event_extraction/standard/bertcrf/bert_crf.py
imports: import logging
  from transformers.models.bert import BertModel, BertPreTrainedModel, BertConfig
  from .crf import *
  from .utils_ee import to_crf_pad, unpad_crf
label_desc: Use image node_id 3 for calling the BertCRFForTokenClassification obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels, pad_token_label_id) and returns: outputs