code: def batched_hyps_to_hypotheses(
      batched_hyps: BatchedHyps,
      alignments: Optional[BatchedAlignments] = None,
  ) -> List[Hypothesis]:
      """
      Convert batched hypotheses to a list of Hypothesis objects.
      Keep this function separate to allow for jit compilation for BatchedHyps class (see tests)
  
      Args:
          batched_hyps: BatchedHyps object
          alignments: BatchedAlignments object, optional; must correspond to BatchedHyps if present
  
      Returns:
          list of Hypothesis objects
      """
      hypotheses = [
          Hypothesis(
              score=batched_hyps.scores[i].item(),
              y_sequence=batched_hyps.transcript[
                  i, : batched_hyps.current_lengths[i]
              ],
              timestep=batched_hyps.timesteps[
                  i, : batched_hyps.current_lengths[i]
              ],
              alignments=None,
              dec_state=None,
          )
          for i in range(batched_hyps.scores.shape[0])
      ]
      if alignments is not None:
          # move all data to cpu to avoid overhead with moving data by chunks
          alignment_lengths = alignments.current_lengths.cpu().tolist()
          if alignments.with_alignments:
              alignment_logits = alignments.logits.cpu()
              alignment_labels = alignments.labels.cpu()
          if alignments.with_frame_confidence:
              frame_confidence = alignments.frame_confidence.cpu()
  
          # for each hypothesis - aggregate alignment using unique_consecutive for time indices (~itertools.groupby)
          for i in range(len(hypotheses)):
              hypotheses[i].alignments = []
              if alignments.with_frame_confidence:
                  hypotheses[i].frame_confidence = []
              _, grouped_counts = torch.unique_consecutive(
                  alignments.timesteps[i, : alignment_lengths[i]],
                  return_counts=True,
              )
              start = 0
              for timestep_cnt in grouped_counts.tolist():
                  if alignments.with_alignments:
                      hypotheses[i].alignments.append(
                          [
                              (
                                  alignment_logits[i, start + j],
                                  alignment_labels[i, start + j],
                              )
                              for j in range(timestep_cnt)
                          ]
                      )
                  if alignments.with_frame_confidence:
                      hypotheses[i].frame_confidence.append(
                          [
                              frame_confidence[i, start + j]
                              for j in range(timestep_cnt)
                          ]
                      )
                  start += timestep_cnt
      return hypotheses
  
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 3 for calling a global function with example usage: batched_hyps_to_hypotheses(batched_hyps, alignments) and returns: hypotheses