code: def _bert_token_tagging_head_fw(
      bert,
      input_ids,
      token_type_ids=None,
      attention_mask=None,
      labels=None,
      head_mask=None,
      valid_ids=None,
  ):
      outputs = bert.bert(
          input_ids,
          token_type_ids=token_type_ids,
          attention_mask=attention_mask,
          head_mask=head_mask,
      )
      sequence_output = outputs[0]
      sequence_output = bert.dropout(sequence_output)
      logits = bert.classifier(sequence_output)
  
      if labels is not None:
          loss_fct = CrossEntropyLoss(ignore_index=0)
          active_positions = valid_ids.view(-1) != 0.0
          active_labels = labels.view(-1)[active_positions]
          active_logits = logits.view(-1, bert.num_labels)[
              active_positions
          ]
          loss = loss_fct(active_logits, active_labels)
          return (
              loss,
              logits,
          )
      return (logits,)
  
imports: import logging
  from typing import List, Union
  import torch
  from torch.nn import CrossEntropyLoss, Dropout, Linear
  from torch.nn import functional
  from torch.utils.data import DataLoader, SequentialSampler, TensorDataset
  from transformers import ROBERTA_PRETRAINED_MODEL_ARCHIVE_MAP, BertForTokenClassification, BertPreTrainedModel, RobertaConfig, RobertaModel, XLNetModel, XLNetPreTrainedModel
  from nlp_architect.data.sequential_tagging import TokenClsInputExample
  from nlp_architect.models.transformers.base_model import InputFeatures, TransformerBase
  from nlp_architect.models.transformers.quantized_bert import QuantizedBertForTokenClassification
  from nlp_architect.utils.metrics import tagging
label_desc: Use image node_id 1 for calling a global function with example usage: _bert_token_tagging_head_fw(bert, input_ids, token_type_ids, attention_mask, labels, head_mask, valid_ids) and returns: logits, loss,  logits