docstr: text_encs/cand_encs/label_inputs masked before training
  In-batch negs training: cand_encs None, label_inputs None, return_loss True
  Hard negs training: cand_encs non-None, label_inputs non-None, return_loss True
      cand_encs = all entities in batch + additional hard negatives
  Inference: cand_encs non-None, label_inputs None, return_loss False
      cand_encs = all entities in DB
  
  cand_encs
     non-None: set of candidate encodings to search in
     None: compute in-batch candidate vectors (used as negatives if train mode)
  label_inputs
     non-None: labels to use for hard negatives training
     None: random negatives training and/or inference
docstr_tok: ["text_encs\/cand_encs\/label_inputs","masked","before","training","In-batch","negs","training",":","cand_encs","None",",","label_inputs","None",",","return_loss","True","Hard","negs","training",":","cand_encs","non-None",",","label_inputs","non-None",",","return_loss","True","cand_encs","=","all","entities","in","batch","+","additional","hard","negatives","Inference",":","cand_encs","non-None",",","label_inputs","None",",","return_loss","False","cand_encs","=","all","entities","in","DB","cand_encs","non-None",":","set","of","candidate","encodings","to","search","in","None",":","compute","in-batch","candidate","vectors","(","used","as","negatives","if","train","mode",")","label_inputs","non-None",":","labels","to","use","for","hard","negatives","training","None",":","random","negatives","training","and\/or","inference"]
code: def forward(
      self,
      context_input,
      cand_input,
      text_encs=None,  # pre-computed mention encoding.
      cand_encs=None,  # pre-computed candidate embeddings
      mention_logits=None,  # pre-computed mention logits
      mention_bounds=None,  # pre-computed mention bounds
      label_input=None,  # labels for passed-in (if hard negatives training)
      gold_mention_bounds=None,
      gold_mention_bounds_mask=None,
      hard_negs_mask=None,  # should be non-none if we are using negs
      return_loss=True,
  ):
      """
      text_encs/cand_encs/label_inputs masked before training
      In-batch negs training: cand_encs None, label_inputs None, return_loss True
      Hard negs training: cand_encs non-None, label_inputs non-None, return_loss True
          cand_encs = all entities in batch + additional hard negatives
      Inference: cand_encs non-None, label_inputs None, return_loss False
          cand_encs = all entities in DB
  
      cand_encs
         non-None: set of candidate encodings to search in
         None: compute in-batch candidate vectors (used as negatives if train mode)
      label_inputs
         non-None: labels to use for hard negatives training
         None: random negatives training and/or inference
      """
      hard_negs = label_input is not None
  
      """
          GET CANDIDATE SCORES
          """
      (
          scores,
          out_mention_logits,
          out_mention_bounds,
      ) = self.score_candidate(
          context_input,
          cand_input,
          hard_negs=hard_negs,
          cand_encs=cand_encs,
          text_encs=text_encs,
          gold_mention_bounds=gold_mention_bounds,
          gold_mention_bounds_mask=gold_mention_bounds_mask,
          hard_negs_mask=hard_negs_mask,
          get_mention_scores=(
              return_loss
              and (mention_logits is None or mention_bounds is None)
          ),
      )
      if mention_logits is None:
          mention_logits = out_mention_logits
      if mention_bounds is None:
          mention_bounds = out_mention_bounds
  
      if not return_loss:
          return None, scores, mention_logits, mention_bounds
  
      """
          COMPUTE MENTION LOSS (TRAINING MODE)
          """
      span_loss = 0
      if mention_logits is not None and mention_bounds is not None:
          N = context_input.size(0)  # batch size
          M = gold_mention_bounds.size(
              1
          )  # num_mentions per instance (just 1, so far)
          # 1 value
          span_loss = self.get_span_loss(
              gold_mention_bounds=gold_mention_bounds,
              gold_mention_bounds_mask=gold_mention_bounds_mask,
              mention_logits=mention_logits,
              mention_bounds=mention_bounds,
          )
  
      """
          COMPUTE EL LOSS (TRAINING MODE)
          """
      if hard_negs:
          """
          Hard negatives (negatives passed in)
          """
          loss_fct = nn.BCEWithLogitsLoss(reduction="mean")
          label_input = label_input[hard_negs_mask]
          # scores: (num_mentions_in_batch,); label_input: (num_mentions_in_batch,)
          loss = loss_fct(scores, label_input.float()) + span_loss
      else:
          """
          Random negatives (use in-batch negatives)
          """
          # scores: (bs*num_mentions [filtered], bs*num_mentions [filtered])
          target = torch.LongTensor(torch.arange(scores.size(1)))
          target = target.to(self.device)
          # log P(entity|mention) + log P(mention) = log [P(entity|mention)P(mention)]
          loss = (
              F.cross_entropy(scores, target, reduction="mean")
              + span_loss
          )
  
      return loss, scores, mention_logits, mention_bounds
  
imports: import os
  import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from tqdm import tqdm
  from collections import OrderedDict
  from pytorch_transformers.modeling_bert import BertPreTrainedModel, BertConfig, BertModel
  from pytorch_transformers.tokenization_bert import BertTokenizer
  from elq.common.ranker_base import BertEncoder, get_model_obj
  from blink.common.optimizer import get_bert_optimizer
  from elq.biencoder.allennlp_span_utils import batched_span_select, batched_index_select
  from elq.biencoder.utils import batch_reshape_mask_left
label_desc: Use image node_id 9 for calling the BiEncoderRanker obj's underlying member method code with example usage: obj.forward(context_input, cand_input, text_encs, cand_encs, mention_logits, mention_bounds, label_input, gold_mention_bounds, gold_mention_bounds_mask, hard_negs_mask, return_loss) and returns: loss,  scores,  mention_logits,  mention_bounds, None,  scores,  mention_logits,  mention_bounds