code: def forward(
      self,
      input_ids,
      attention_mask=None,
      token_type_ids=None,
      labels=None,
      valid_ids=None,
      attention_mask_label=None,
      device=None,
  ):
      sequence_output = self.bert(
          input_ids=input_ids,
          attention_mask=attention_mask,
          token_type_ids=token_type_ids,
          head_mask=None,
      )[0]
      batch_size, max_len, feat_dim = sequence_output.shape
      valid_output = torch.zeros(
          batch_size,
          max_len,
          feat_dim,
          dtype=torch.float32,
          device=device,
      )
      for i in range(batch_size):
          jj = -1
          for j in range(max_len):
              if valid_ids[i][j].item() == 1:
                  jj += 1
                  valid_output[i][jj] = sequence_output[i][j]
      sequence_output = self.dropout(valid_output)
      logits = self.classifier(sequence_output)
  
      if labels is not None:
          loss_fct = nn.CrossEntropyLoss(ignore_index=0)
          if attention_mask_label is not None:
              active_loss = attention_mask_label.view(-1) == 1
              active_logits = logits.view(-1, self.num_labels)[
                  active_loss
              ]
              active_labels = labels.view(-1)[active_loss]
              loss = loss_fct(active_logits, active_labels)
          else:
              loss = loss_fct(
                  logits.view(-1, self.num_labels), labels.view(-1)
              )
          return loss
      else:
          return logits
  
file_path: DeepKE/example/ner/standard/run_bert.py
imports: import json
  import logging
  import os
  import random
  import numpy
  import torch
  import torch.nn.functional
  from transformers import AdamW, BertConfig, BertForTokenClassification, BertTokenizer, get_linear_schedule_with_warmup
  from torch import nn
  from torch.utils.data import DataLoader, RandomSampler, SequentialSampler, TensorDataset
  from tqdm import tqdm, trange
  from seqeval.metrics import classification_report
  import hydra
  from hydra import utils
  from deepke.name_entity_re.standard import *
  import wandb
label_desc: Use image node_id 1 for calling the TrainNer obj's underlying member method code with example usage: obj.forward(input_ids, attention_mask, token_type_ids, labels, valid_ids, attention_mask_label, device) and returns: loss, logits