code: def select_k_expansions(
      hyps: List[Hypothesis],
      topk_idxs: torch.Tensor,
      topk_logps: torch.Tensor,
      gamma: float,
      beta: int,
  ) -> List[Tuple[int, Hypothesis]]:
      """
      Obtained from https://github.com/espnet/espnet
  
      Return K hypotheses candidates for expansion from a list of hypothesis.
      K candidates are selected according to the extended hypotheses probabilities
      and a prune-by-value method. Where K is equal to beam_size + beta.
  
      Args:
          hyps: Hypotheses.
          topk_idxs: Indices of candidates hypothesis. Shape = [B, num_candidates]
          topk_logps: Log-probabilities for hypotheses expansions. Shape = [B, V + 1]
          gamma: Allowed logp difference for prune-by-value method.
          beta: Number of additional candidates to store.
  
      Return:
          k_expansions: Best K expansion hypotheses candidates.
      """
      k_expansions = []
  
      for i, hyp in enumerate(hyps):
          hyp_i = [
              (int(k), hyp.score + float(v))
              for k, v in zip(topk_idxs[i], topk_logps[i])
          ]
          k_best_exp_val = max(hyp_i, key=lambda x: x[1])
  
          k_best_exp_idx = k_best_exp_val[0]
          k_best_exp = k_best_exp_val[1]
  
          expansions = sorted(
              filter(lambda x: (k_best_exp - gamma) <= x[1], hyp_i),
              key=lambda x: x[1],
          )
  
          if len(expansions) > 0:
              k_expansions.append(expansions)
          else:
              k_expansions.append([(k_best_exp_idx, k_best_exp)])
  
      return k_expansions
  
file_path: NeMo/nemo/collections/asr/parts/utils/rnnt_utils.py
imports: from dataclasses import dataclass, field
  from typing import Any, Dict, List, Optional, Tuple, Union
  import torch
label_desc: Use image node_id 2 for calling a global function with example usage: select_k_expansions(hyps, topk_idxs, topk_logps, gamma, beta) and returns: k_expansions