docstr: If gold_mention_bounds is set, returns mention embeddings of passed-in mention bounds
  Otherwise, uses top-scoring mentions
docstr_tok: ["If","gold_mention_bounds","is","set",",","returns","mention","embeddings","of","passed-in","mention","bounds","Otherwise",",","uses","top-scoring","mentions"]
code: def forward_ctxt(
      self,
      token_idx_ctxt,
      segment_idx_ctxt,
      mask_ctxt,
      gold_mention_bounds=None,
      gold_mention_bounds_mask=None,
      num_cand_mentions=50,
      topK_threshold=-4.5,
      get_mention_scores=True,
  ):
      """
      If gold_mention_bounds is set, returns mention embeddings of passed-in mention bounds
      Otherwise, uses top-scoring mentions
      """
  
      if self.mention_aggregation_type is None:
          """
          OLD system: don't do mention aggregation (use tokens around mention)
          """
          embedding_ctxt = self.context_encoder(
              token_idx_ctxt,
              segment_idx_ctxt,
              mask_ctxt,
          )
          # linear mapping to correct context length
          if self.linear_compression is not None:
              embedding_ctxt = self.linear_compression(embedding_ctxt)
          return embedding_ctxt, None, None, None
  
      else:
          """
          NEW system: aggregate mention tokens
          """
          # (bs, seqlen, embed_size)
          raw_ctxt_encoding = self.get_raw_ctxt_encoding(
              token_idx_ctxt,
              segment_idx_ctxt,
              mask_ctxt,
          )
  
          top_mention_bounds = None
          top_mention_logits = None
          extra_rets = {}
          if get_mention_scores:
              (
                  mention_logits,
                  mention_bounds,
              ) = self.get_ctxt_mention_scores(
                  token_idx_ctxt,
                  segment_idx_ctxt,
                  mask_ctxt,
                  raw_ctxt_encoding,
              )
              extra_rets["all_mention_logits"] = mention_logits
              extra_rets["all_mention_bounds"] = mention_bounds
              if gold_mention_bounds is None:
                  (
                      top_mention_logits,
                      top_mention_bounds,
                      top_mention_mask,
                      all_mention_mask,
                  ) = self.prune_ctxt_mentions(
                      mention_logits,
                      mention_bounds,
                      num_cand_mentions,
                      topK_threshold,
                  )
                  extra_rets[
                      "mention_logits"
                  ] = top_mention_logits.view(-1)
                  extra_rets["all_mention_mask"] = all_mention_mask
  
          if top_mention_bounds is None:
              # use gold mention
              top_mention_bounds = gold_mention_bounds
              top_mention_mask = gold_mention_bounds_mask
  
          assert top_mention_bounds is not None
          assert top_mention_mask is not None
  
          # (bs, num_pred_mentions OR num_gold_mentions, embed_size)
          embedding_ctxt = self.get_ctxt_embeds(
              raw_ctxt_encoding,
              top_mention_bounds,
          )
          # for merging dataparallel, only 1st dimension can differ...
          return {
              "mention_reps": embedding_ctxt.view(
                  -1, embedding_ctxt.size(-1)
              ),
              "mention_bounds": top_mention_bounds.view(
                  -1, top_mention_bounds.size(-1)
              ),
              "mention_masks": top_mention_mask.view(-1),
              "mention_dims": torch.tensor(top_mention_mask.size())
              .unsqueeze(0)
              .to(embedding_ctxt.device),
              **extra_rets,
          }
  
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 6 for calling the BiEncoderModule obj's underlying member method code with example usage: obj.forward_ctxt(token_idx_ctxt, segment_idx_ctxt, mask_ctxt, gold_mention_bounds, gold_mention_bounds_mask, num_cand_mentions, topK_threshold, get_mention_scores) and returns: embedding_ctxt,  None,  None,  None, dict