class_docstr: Tests various generation functionalities.
  
  NOTE: Currently incomplete.
class_docstr_tok: ["Tests","various","generation","functionalities",".","NOTE",":","Currently","incomplete","."]
docstr: Unit test `select_paths` for different decoding schemes.
docstr_tok: ["Unit","test","`","select_paths","`","for","different","decoding","schemes","."]
code: def test_tree_search(self):
      """
      Unit test `select_paths` for different decoding schemes.
      """
      parser = ParlaiParser()
      DictionaryAgent.add_cmdline_args(parser, partial_opt=None)
      opt = parser.parse_args([])
      dictionary = DictionaryAgent(opt)
      tests = {
          "greedy": {
              "obj": GreedySearch(beam_size=1, verbose=True),
              "logprobs": torch.Tensor([[-1.0, -1.0, -0.1, -0.3]]),
              "prior_scores": torch.Tensor([-0.5]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0]),
                  "token_ids": torch.LongTensor([2]),
                  "scores": torch.Tensor([-0.6]),
                  "token_details": [
                      {
                          "token_logprob": math.log(0.3800),
                          "token_rank": 0,
                      }
                  ],
              },
          },
          "beam_with_one_beam": {
              "obj": BeamSearch(beam_size=1, verbose=True),
              "logprobs": torch.Tensor([[-1.0, -1.0, -0.1, -0.3]]),
              "prior_scores": torch.Tensor([-0.5]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0]),
                  "token_ids": torch.LongTensor([2]),
                  "scores": torch.Tensor([-0.6]),
                  "token_details": [
                      {
                          "token_logprob": math.log(0.3800),
                          "token_rank": 0,
                      }
                  ],
              },
          },
          "beam_with_multiple_beams": {
              "obj": BeamSearch(beam_size=2, verbose=True),
              "logprobs": torch.Tensor(
                  [[-0.1, -2.0, -3.0, -3.0], [-1.0, -1.0, -0.2, -0.3]]
              ),
              "prior_scores": torch.Tensor([-1.0, -0.5]),
              # logprobs + prior_scores = [[-1.1,-3.,-4.,-4.],[-1.5,-1.5,-0.7,-0.8]]
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([1, 1]),
                  "token_ids": torch.LongTensor([2, 3]),
                  "scores": torch.Tensor([-0.7, -0.8]),
                  "token_details": [
                      {
                          "token_logprob": math.log(0.3567),
                          "token_rank": 0,
                      },
                      {
                          "token_logprob": math.log(0.3228),
                          "token_rank": 1,
                      },
                  ],
              },
          },
          "topk_with_one_beam": {
              "obj": TopKSampling(beam_size=1, k=3, verbose=True),
              "logprobs": torch.Tensor(
                  [[-float("inf"), -0.5, -float("inf"), -float("inf")]]
              ),
              "prior_scores": torch.Tensor([-3.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0]),
                  "token_ids": torch.LongTensor([1]),
                  "scores": torch.Tensor([-3.5]),
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0}
                  ],
              },
          },
          "topk_with_multiple_beams": {
              "obj": TopKSampling(beam_size=2, k=3, verbose=True),
              "logprobs": torch.Tensor(
                  [
                      [
                          -float("inf"),
                          -0.5,
                          -float("inf"),
                          -float("inf"),
                      ],
                      [
                          -float("inf"),
                          -float("inf"),
                          -0.6,
                          -float("inf"),
                      ],
                  ]
              ),
              "prior_scores": torch.Tensor([-3.0, -2.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0, 1]),
                  "token_ids": torch.LongTensor([1, 2]),
                  "scores": torch.Tensor([-3.5, -2.6]),
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0},
                      {"token_logprob": 0.0, "token_rank": 0},
                  ],
              },
          },
          "nucleus_with_one_beam": {
              "obj": NucleusSampling(beam_size=1, p=0.9, verbose=True),
              "logprobs": torch.Tensor(
                  [[-float("inf"), -0.5, -float("inf"), -float("inf")]]
              ),
              "prior_scores": torch.Tensor([-3.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0]),
                  "token_ids": torch.LongTensor([1]),
                  "scores": torch.Tensor(
                      [-3.0]
                  ),  # the -0.5 logprob normalizes to 0 in truncated distribution
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0}
                  ],
              },
          },
          "nucleus_with_multiple_beams": {
              "obj": NucleusSampling(beam_size=2, p=0.9, verbose=True),
              "logprobs": torch.Tensor(
                  [
                      [
                          -float("inf"),
                          -0.5,
                          -float("inf"),
                          -float("inf"),
                      ],
                      [
                          -float("inf"),
                          -float("inf"),
                          -0.6,
                          -float("inf"),
                      ],
                  ]
              ),
              "prior_scores": torch.Tensor([-3.0, -2.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0, 1]),
                  "token_ids": torch.LongTensor([1, 2]),
                  "scores": torch.Tensor(
                      [-3.0, -2.0]
                  ),  # the -0.5, -0.6 logprobs normalize to 0 in truncated distributions
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0},
                      {"token_logprob": 0.0, "token_rank": 0},
                  ],
              },
          },
          "factual_nucleus_with_one_beam": {
              "obj": FactualNucleusSampling(
                  p=0.9,
                  lambda_decay=0.9,
                  omega_bound=0.3,
                  p_reset=True,
                  beam_size=1,
                  verbose=True,
                  dict=dictionary,
              ),
              "logprobs": torch.Tensor(
                  [[-float("inf"), -0.5, -float("inf"), -float("inf")]]
              ),
              "prior_scores": torch.Tensor([-3.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0]),
                  "token_ids": torch.LongTensor([1]),
                  "scores": torch.Tensor(
                      [-3.0]
                  ),  # the -0.5 logprob normalizes to 0 in truncated distribution
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0}
                  ],
              },
          },
          "factual_nucleus_with_multiple_beams": {
              "obj": FactualNucleusSampling(
                  p=0.9,
                  lambda_decay=0.9,
                  omega_bound=0.3,
                  p_reset=True,
                  beam_size=2,
                  verbose=True,
                  dict=dictionary,
              ),
              "logprobs": torch.Tensor(
                  [
                      [
                          -float("inf"),
                          -0.5,
                          -float("inf"),
                          -float("inf"),
                      ],
                      [
                          -float("inf"),
                          -float("inf"),
                          -0.6,
                          -float("inf"),
                      ],
                  ]
              ),
              "prior_scores": torch.Tensor([-3.0, -2.0]),
              "expected_result": {
                  "hypothesis_ids": torch.LongTensor([0, 1]),
                  "token_ids": torch.LongTensor([1, 2]),
                  "scores": torch.Tensor(
                      [-3.0, -2.0]
                  ),  # the -0.5, -0.6 logprobs normalize to 0 in truncated distributions
                  "token_details": [
                      {"token_logprob": 0.0, "token_rank": 0},
                      {"token_logprob": 0.0, "token_rank": 0},
                  ],
              },
          },
      }
  
      for test_name, test_data in tests.items():
          path_selection = test_data["obj"].select_paths(
              test_data["logprobs"], test_data["prior_scores"], None
          )
          expected_result = test_data["expected_result"]
  
          assert torch.equal(
              path_selection.hypothesis_ids,
              expected_result["hypothesis_ids"],
          ), f"failed test_tree_search for test {test_name} on field hypothesis_ids"
          assert torch.equal(
              path_selection.token_ids, expected_result["token_ids"]
          ), f"failed test_tree_search for test {test_name} on field token_ids"
          assert torch.allclose(
              path_selection.scores, expected_result["scores"]
          ), f"failed test_tree_search for test {test_name} on field scores"
  
          assert len(path_selection.token_details) == len(
              expected_result["token_details"]
          ), f"failed test_tree_search for test {test_name} on field token_details"
          for token_details, expected_token_details in zip(
              path_selection.token_details,
              expected_result["token_details"],
          ):
              assert math.isclose(
                  token_details["token_logprob"],
                  expected_token_details["token_logprob"],
                  abs_tol=1e-3,
              ), f"failed test_tree_search for test {test_name} on field token_details"
              assert (
                  token_details["token_rank"]
                  == expected_token_details["token_rank"]
              ), f"failed test_tree_search for test {test_name} on field token_details"
  
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 4 for calling the TestGeneration obj's underlying member method code with example usage: obj.test_tree_search() without return types