class_docstr: Generate Dense Embeddings.
class_docstr_tok: ["Generate","Dense","Embeddings","."]
docstr: Encode passages with model, using candidate encoder.
  
  :param agent:
      parlai agent
  :param passages:
      passages to encode
  
  :return encodings:
      return passage encodings
docstr_tok: ["Encode","passages","with","model",",","using","candidate","encoder",".",":","param","agent",":","parlai","agent",":","param","passages",":","passages","to","encode",":","return","encodings",":","return","passage","encodings"]
code: def encode_passages(
      self,
      agent: TorchRankerAgent,
      passages: List[Tuple[str, str, str]],
  ) -> Tuple[torch.Tensor, List[str]]:
      """
      Encode passages with model, using candidate encoder.
  
      :param agent:
          parlai agent
      :param passages:
          passages to encode
  
      :return encodings:
          return passage encodings
      """
      agent.model.eval()
      n = len(passages)
      bsz = self.opt["batchsize"]
      total_enc = 0
      results: List[torch.Tensor] = []
      results_ids: List[str] = []
  
      for batch_start in range(0, n, bsz):
          batch_passages = passages[batch_start : batch_start + bsz]
          batch_msgs = [
              Message(
                  {
                      "text_vec": agent._check_truncate(
                          agent.dict.txt2vec(f"{title} {text}"),
                          agent.text_truncate,
                      )
                  }
              )
              for _, text, title in batch_passages
          ]
          # we call batchify here rather than _pad_tensor directly.
          batch = agent.batchify(batch_msgs)
          if self.use_cuda:
              batch = batch.to("cuda")
          with torch.no_grad():
              if isinstance(agent, TransformerRankerAgent):
                  _, encoding = agent.model(None, None, batch.text_vec)
              else:
                  assert isinstance(
                      agent, DropoutPolyAgent
                  ) or isinstance(agent, PolyencoderAgent)
                  _, _, encoding = agent.model(
                      cand_tokens=batch.text_vec.unsqueeze(1)
                  )
                  encoding = encoding.squeeze(1)
  
          ids = [r[0] for r in batch_passages]
          assert len(ids) == encoding.size(0)
          results.append(encoding.cpu())
          results_ids += ids
  
          total_enc += len(ids)
          if total_enc % (10 * bsz) == 0:
              logging.info(f"Encoded {total_enc} out of {n} passages")
  
      return torch.cat(results).cpu(), results_ids
  
imports: import os
  import torch
  import torch.cuda
  from typing import List, Tuple
  from parlai.agents.transformer.transformer import TransformerRankerAgent
  from parlai.agents.transformer.polyencoder import PolyencoderAgent
  from parlai.core.agents import create_agent_from_model_file, create_agent
  from parlai.core.build_data import modelzoo_path
  from parlai.core.message import Message
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.script import ParlaiScript
  from parlai.core.torch_ranker_agent import TorchRankerAgent
  import parlai.utils.logging
  from parlai.agents.transformer.dropout_poly import DropoutPolyAgent
  from parlai.agents.rag.retrievers import load_passages_list
  import parlai.agents.rag.dpr
label_desc: Use image node_id 3 for calling the Generator obj's underlying member method code with example usage: obj.encode_passages(agent, passages) and returns: torch,  results_ids