docstr: Modify logprobs in PACER.
  
  The way it works:
  
  1. With frequency r, select a token x_i+1 to re-rank.
  2. Generate word probabilities for token x_i+1.
  3. Examine top k words {x_j | score(x_j) in top_k(P(x_i+1 | x_0,...,x_i))}; use classifier to predict P(a|x1, ..., x_i, x_j)
  4. Rescore top k words via multiplication, re-normalize, and advance the generation.
  
  :param logprobs:
      initial token probabilities
  
  :return modified:
      return the modified log probabilities according to PACER
docstr_tok: ["Modify","logprobs","in","PACER",".","The","way","it","works",":","1",".","With","frequency","r",",","select","a","token","x_i+1","to","re-rank",".","2",".","Generate","word","probabilities","for","token","x_i+1",".","3",".","Examine","top","k","words","{","x_j","|","score","(","x_j",")","in","top_k","(","P","(","x_i+1","|","x_0",",","...",",","x_i",")",")","}",";","use","classifier","to","predict","P","(","a|x1",",","...",",","x_i",",","x_j",")","4",".","Rescore","top","k","words","via","multiplication",",","re-normalize",",","and","advance","the","generation",".",":","param","logprobs",":","initial","token","probabilities",":","return","modified",":","return","the","modified","log","probabilities","according","to","PACER"]
code: def modify_logprobs(self, logprobs: torch.Tensor) -> torch.Tensor:
      """
      Modify logprobs in PACER.
  
      The way it works:
  
      1. With frequency r, select a token x_i+1 to re-rank.
      2. Generate word probabilities for token x_i+1.
      3. Examine top k words {x_j | score(x_j) in top_k(P(x_i+1 | x_0,...,x_i))}; use classifier to predict P(a|x1, ..., x_i, x_j)
      4. Rescore top k words via multiplication, re-normalize, and advance the generation.
  
      :param logprobs:
          initial token probabilities
  
      :return modified:
          return the modified log probabilities according to PACER
      """
      if random.random() > self.frequency:
          return logprobs
      vals, inds = logprobs.topk(self.n_toks, dim=-1, sorted=False)
      new_probs = logprobs.clone().fill_(neginf(logprobs.dtype))
      # Construct partial hypotheses for each beam for each top K tokens
      batch_hyps = [
          h
          for i in range(len(self.partial_hyps))
          for h in [
              self.agent._v2t(self.partial_hyps[i][1:].tolist() + [ind])
              for ind in inds[i]
          ]
      ]
      # Classify all beam outputs
      predictor_outputs = self.classifier.batch_classify(
          [self.context_str] * self.n_toks * logprobs.size(0),
          batch_hyps,
      )
      # Extract RPA scores
      log_predictor_scores = (
          torch.stack(
              [
                  F.log_softmax(pred["sorted_scores"].float(), dim=0)[
                      pred["text_candidates"].index(self.character)
                  ]
                  for pred in predictor_outputs
              ]
          )
          .to(vals.device)
          .view(vals.size())
      )
      # "Multiply" Probabilities (in log space...)
      scores = vals + log_predictor_scores
      for i in range(new_probs.size(0)):
          new_probs[i, inds[i]] = scores[i]
      return F.log_softmax(new_probs, dim=-1, dtype=torch.float32)
  
imports: import random
  import torch
  import torch.nn.functional
  from typing import Optional, Any, Dict, List
  from parlai.agents.rag.retrievers import clean_vec
  from parlai.agents.transformer.transformer import TransformerGeneratorAgent
  from parlai.core.opt import Opt
  from parlai.core.params import ParlaiParser
  from parlai.core.torch_generator_agent import TreeSearch, GreedySearch, BeamSearch, DelayedBeamSearch, TopKSampling, NucleusSampling, TSType
  import parlai.utils.logging
  from parlai.utils.torch import neginf
  from projects.light_whoami.agents.rpa_rerank import RPAReranker, RPARerankAgent, LongRPARerankAgent
  from projects.light_whoami.task.utils import extract_characters
  from projects.msc.agents.long_tga import TransformerVariantAgent
  from parlai.agents.reranker.reranker import AbstractReranker
label_desc: Use image node_id 5 for calling the PacerTreeSearchMixin obj's underlying member method code with example usage: obj.modify_logprobs(logprobs) and returns: F, logprobs