class_docstr: Tests various generation functionalities.
  
  NOTE: Currently incomplete.
class_docstr_tok: ["Tests","various","generation","functionalities",".","NOTE",":","Currently","incomplete","."]
docstr: Test functionality of token level probability + ranking logging.
  
  Regression for all inference types: 'beam', 'greedy', 'topk', 'nucleus',
  'delayedbeam'
docstr_tok: ["Test","functionality","of","token","level","probability","+","ranking","logging",".","Regression","for","all","inference","types",":","'beam","'",",","'greedy","'",",","'topk","'",",","'nucleus","'",",","'delayedbeam","'"]
code: def test_token_level_loss_logging(self):
      """
      Test functionality of token level probability + ranking logging.
  
      Regression for all inference types: 'beam', 'greedy', 'topk', 'nucleus',
      'delayedbeam'
      """
      inference_types = [
          "beam",
          "greedy",
          "topk",
          "nucleus",
          "factual_nucleus",
          "delayedbeam",
      ]
      gold_data = {
          "beam": {
              "text_token_info": [
                  (
                      "__start__",
                      {"token_logprob": 0.0, "token_rank": 0},
                  ),
                  (
                      "5",
                      {
                          "token_logprob": math.log(0.999),
                          "token_rank": 0,
                      },
                  ),
                  (
                      "__end__",
                      {
                          "token_logprob": math.log(0.999),
                          "token_rank": 0,
                      },
                  ),
              ],
              "extra_args": ["--beam-size", "3"],
          },
          "greedy": {
              "text_token_info": [
                  (
                      "__start__",
                      {"token_logprob": 0.0, "token_rank": 0},
                  ),
                  (
                      "5",
                      {
                          "token_logprob": math.log(0.999),
                          "token_rank": 0,
                      },
                  ),
                  (
                      "__end__",
                      {
                          "token_logprob": math.log(0.999),
                          "token_rank": 0,
                      },
                  ),
              ],
              "extra_args": [],
          },
          # sampling based token selection will produce non-deterministic output, so we can't do data regression
          "topk": {"extra_args": ["--topk", "2"]},
          "topk_multiple_beams": {
              "extra_args": ["--topk", "2", "--beam-size", "5"]
          },
          # sampling based token selection will produce non-deterministic output, so we can't do data regression
          "nucleus": {"extra_args": ["--topp", "0.3"]},
          "nucleus_multiple_beams": {
              "extra_args": ["--topp", "0.3", "--beam-size", "5"]
          },
          "factual_nucleus": {"extra_args": ["--topp", "0.3"]},
          "factual_nucleus_multiple_beams": {
              "extra_args": ["--topp", "0.3", "--beam-size", "5"]
          },
          # sampling based token selection will produce non-deterministic output, so we can't do data regression
          "delayedbeam": {
              "extra_args": ["--topk", "2", "--beam-delay", "2"]
          },
      }
  
      for inference_type in inference_types:
          args = [
              "--model-file",
              "zoo:unittest/transformer_generator2/model",
              "--inference",
              inference_type,
              "--truncate",
              "1024",
              "-v",
          ] + gold_data[inference_type]["extra_args"]
  
          pp = ParlaiParser(True, True)
          agent = create_agent(pp.parse_args(args), True)
          obs = {"text": "5", "episode_done": False}
          agent.observe(obs)
          act = agent.act()
  
          if "text_token_info" in gold_data[inference_type]:
              for i, tok_data in enumerate(act["text_token_info"]):
                  assert (
                      gold_data[inference_type]["text_token_info"][i][0]
                      == tok_data[0]
                  ), f"failed token prediction for inference type {inference_type} at token {gold_data[inference_type]['text_token_info'][i][0]}"
                  assert math.isclose(
                      gold_data[inference_type]["text_token_info"][i][
                          1
                      ]["token_logprob"],
                      tok_data[1]["token_logprob"],
                      abs_tol=1e-3,
                  ), f"failed token log-probability prediction for inference type {inference_type} at token {gold_data[inference_type]['text_token_info'][i][0]}"
                  assert math.isclose(
                      gold_data[inference_type]["text_token_info"][i][
                          1
                      ]["token_rank"],
                      tok_data[1]["token_rank"],
                  ), f"failed token rank prediction for inference type {inference_type} at token {gold_data[inference_type]['text_token_info'][i][0]}"
  
imports: import unittest
  import math
  import torch
  from parlai.core.agents import create_agent
  from parlai.core.dict import DictionaryAgent
  import parlai.utils.testing
  from parlai.core.params import ParlaiParser
  from parlai.core.torch_generator_agent import BeamSearch, GreedySearch, NucleusSampling, FactualNucleusSampling, TopKSampling, TorchGeneratorAgent
  from parlai.agents.test_agents.transformer_generator_prefix import PREFIX_TEXT
label_desc: Use image node_id 3 for calling the TestGeneration obj's underlying member method code with example usage: obj.test_token_level_loss_logging() without return types