class_docstr: Class with model for ranking of entities by context and description
  Args:
      emb_size: entity embedding size
      block_size: size of block in bilinear layer
      encoder_save_path: path to save the encoder checkpoint
      bilinear_save_path: path to save bilinear layer checkpoint
      pretrained_bert: pretrained Bert checkpoint path or key title (e.g. "bert-base-uncased")
      bert_config_file: path to Bert configuration file, or None, if `pretrained_bert` is a string name
      device: device to use
code: def forward(
      self,
      q_input_ids: Tensor,
      q_attention_mask: Tensor,
      c_input_ids: Tensor,
      c_attention_mask: Tensor,
      entity_tokens_pos: List,
      labels: List[int] = None,
  ) -> Union[Tuple[Any, Tensor], Tuple[Tensor]]:
      entity_emb = self.encoder(
          input_ids=q_input_ids,
          attention_mask=q_attention_mask,
          entity_tokens_pos=entity_tokens_pos,
      )
      c_cls_emb = self.encoder(
          input_ids=c_input_ids, attention_mask=c_attention_mask
      )
      softmax_scores, log_softmax = self.bilinear_ranker(
          entity_emb, c_cls_emb
      )
  
      if labels is not None:
          labels_one_hot = [[0.0, 0.0] for _ in labels]
          for i in range(len(labels)):
              labels_one_hot[i][labels[i]] = 1.0
          labels_one_hot = torch.Tensor(labels_one_hot).to(self.device)
  
          bs, dim = labels_one_hot.shape
          per_sample_loss = (
              -torch.bmm(
                  labels_one_hot.view(bs, 1, dim),
                  log_softmax.view(bs, dim, 1),
              )
              .squeeze(2)
              .squeeze(1)
          )
          loss = torch.mean(per_sample_loss)
          return loss, softmax_scores
      else:
          return softmax_scores
  
imports: from logging import getLogger
  from pathlib import Path
  from typing import List, Optional, Dict, Tuple, Union, Any
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from torch import Tensor
  from transformers import AutoConfig, AutoTokenizer, AutoModel
  from deeppavlov.core.commands.utils import expand_path
  from deeppavlov.core.common.errors import ConfigError
  from deeppavlov.core.common.registry import register
  from deeppavlov.core.models.torch_model import TorchModel
  from deeppavlov.models.preprocessors.torch_transformers_preprocessor import TorchTransformersEntityRankerPreprocessor
label_desc: Use image node_id 2 for calling the SiameseBertElModel obj's underlying member method code with example usage: obj.forward(q_input_ids, q_attention_mask, c_input_ids, c_attention_mask, entity_tokens_pos, labels) and returns: loss,  softmax_scores, softmax_scores