class_docstr: Abstract TorchRankerAgent class; only meant to be extended.
  
  TorchRankerAgents aim to provide convenient functionality for building ranking
  models. This includes:
  
  - Training/evaluating on candidates from a variety of sources.
  - Computing hits@1, hits@5, mean reciprical rank (MRR), and other metrics.
  - Caching representations for fast runtime when deploying models to production.
class_docstr_tok: ["Abstract","TorchRankerAgent","class",";","only","meant","to","be","extended",".","TorchRankerAgents","aim","to","provide","convenient","functionality","for","building","ranking","models",".","This","includes",":","-","Training\/evaluating","on","candidates","from","a","variety","of","sources",".","-","Computing","hits","@","1",",","hits","@","5",",","mean","reciprical","rank","(","MRR",")",",","and","other","metrics",".","-","Caching","representations","for","fast","runtime","when","deploying","models","to","production","."]
docstr: Load a set of fixed candidates and their vectors (or vectorize them here).
  
  self.fixed_candidates will contain a [num_cands] list of strings
  self.fixed_candidate_vecs will contain a [num_cands, seq_len] LongTensor
  
  See the note on the --fixed-candidate-vecs flag for an explanation of the
  'reuse', 'replace', or path options.
  
  Note: TorchRankerAgent by default converts candidates to vectors by vectorizing
  in the common sense (i.e., replacing each token with its index in the
  dictionary). If a child model wants to additionally perform encoding, it can
  overwrite the vectorize_fixed_candidates() method to produce encoded vectors
  instead of just vectorized ones.
docstr_tok: ["Load","a","set","of","fixed","candidates","and","their","vectors","(","or","vectorize","them","here",")",".","self.fixed_candidates","will","contain","a","[","num_cands","]","list","of","strings","self.fixed_candidate_vecs","will","contain","a","[","num_cands",",","seq_len","]","LongTensor","See","the","note","on","the","--","fixed-candidate-vecs","flag","for","an","explanation","of","the","'reuse","'",",","'replace","'",",","or","path","options",".","Note",":","TorchRankerAgent","by","default","converts","candidates","to","vectors","by","vectorizing","in","the","common","sense","(","i.e.",",","replacing","each","token","with","its","index","in","the","dictionary",")",".","If","a","child","model","wants","to","additionally","perform","encoding",",","it","can","overwrite","the","vectorize_fixed_candidates","(",")","method","to","produce","encoded","vectors","instead","of","just","vectorized","ones","."]
code: def set_fixed_candidates(self, shared):
      """
      Load a set of fixed candidates and their vectors (or vectorize them here).
  
      self.fixed_candidates will contain a [num_cands] list of strings
      self.fixed_candidate_vecs will contain a [num_cands, seq_len] LongTensor
  
      See the note on the --fixed-candidate-vecs flag for an explanation of the
      'reuse', 'replace', or path options.
  
      Note: TorchRankerAgent by default converts candidates to vectors by vectorizing
      in the common sense (i.e., replacing each token with its index in the
      dictionary). If a child model wants to additionally perform encoding, it can
      overwrite the vectorize_fixed_candidates() method to produce encoded vectors
      instead of just vectorized ones.
      """
      if shared:
          self.fixed_candidates = shared["fixed_candidates"]
          self.fixed_candidate_vecs = shared["fixed_candidate_vecs"]
          self.fixed_candidate_encs = shared["fixed_candidate_encs"]
          self.num_fixed_candidates = shared["num_fixed_candidates"]
      else:
          self.num_fixed_candidates = 0
          opt = self.opt
          cand_path = self.fixed_candidates_path
          if "fixed" in (self.candidates, self.eval_candidates):
              if not cand_path:
                  # Attempt to get a standard candidate set for the given task
                  path = self.get_task_candidates_path()
                  if path:
                      logging.info(
                          f"setting fixed_candidates path to: {path}"
                      )
                      self.fixed_candidates_path = path
                      cand_path = self.fixed_candidates_path
              # Load candidates
              logging.info(
                  f"Loading fixed candidate set from {cand_path}"
              )
              with PathManager.open(
                  cand_path, "r", encoding="utf-8"
              ) as f:
                  cands = [line.strip() for line in f.readlines()]
              # Load or create candidate vectors
              if PathManager.exists(self.opt["fixed_candidate_vecs"]):
                  vecs_path = opt["fixed_candidate_vecs"]
                  vecs = self.load_candidates(vecs_path)
              else:
                  setting = self.opt["fixed_candidate_vecs"]
                  model_dir, model_file = os.path.split(
                      self.opt["model_file"]
                  )
                  model_name = os.path.splitext(model_file)[0]
                  cands_name = os.path.splitext(
                      os.path.basename(cand_path)
                  )[0]
                  vecs_path = os.path.join(
                      model_dir,
                      ".".join([model_name, cands_name, "vecs"]),
                  )
                  if setting == "reuse" and PathManager.exists(
                      vecs_path
                  ):
                      vecs = self.load_candidates(vecs_path)
                  else:  # setting == 'replace' OR generating for the first time
                      vecs = self._make_candidate_vecs(cands)
                      self._save_candidates(vecs, vecs_path)
  
              self.fixed_candidates = cands
              self.num_fixed_candidates = len(self.fixed_candidates)
              self.fixed_candidate_vecs = vecs
              if self.use_cuda:
                  self.fixed_candidate_vecs = (
                      self.fixed_candidate_vecs.cuda()
                  )
  
              if self.encode_candidate_vecs:
                  # candidate encodings are fixed so set them up now
                  enc_path = os.path.join(
                      model_dir,
                      ".".join([model_name, cands_name, "encs"]),
                  )
                  if setting == "reuse" and PathManager.exists(
                      enc_path
                  ):
                      encs = self.load_candidates(
                          enc_path, cand_type="encodings"
                      )
                  else:
                      encs = self._make_candidate_encs(
                          self.fixed_candidate_vecs
                      )
                      self._save_candidates(
                          encs, path=enc_path, cand_type="encodings"
                      )
                  self.fixed_candidate_encs = encs
                  if self.use_cuda:
                      self.fixed_candidate_encs = (
                          self.fixed_candidate_encs.cuda()
                      )
                  if self.fp16:
                      self.fixed_candidate_encs = (
                          self.fixed_candidate_encs.half()
                      )
                  else:
                      self.fixed_candidate_encs = (
                          self.fixed_candidate_encs.float()
                      )
              else:
                  self.fixed_candidate_encs = None
  
          else:
              self.fixed_candidates = None
              self.fixed_candidate_vecs = None
              self.fixed_candidate_encs = None
  
imports: from typing import Optional
  from parlai.core.params import ParlaiParser
  from typing import Dict, Any
  from abc import abstractmethod
  from itertools import islice
  import os
  from tqdm import tqdm
  import random
  import torch
  from parlai.core.opt import Opt
  from parlai.utils.distributed import is_distributed
  from parlai.core.torch_agent import TorchAgent, Output
  from parlai.utils.misc import warn_once
  from parlai.utils.io import PathManager
  from parlai.utils.torch import padded_3d, total_parameters, trainable_parameters, PipelineHelper
  from parlai.utils.fp16 import FP16SafeCrossEntropy
  from parlai.core.metrics import AverageMetric
  import parlai.utils.logging
label_desc: Use image node_id 20 for calling the TorchRankerAgent obj's underlying member method code with example usage: obj.set_fixed_candidates(shared) without return types