code: def nbest_am_lm_scores(
      lats: k2.Fsa,
      num_paths: int,
      device: str = "cuda",
      batch_size: int = 500,
  ):
      """Compute am scores with word_seqs
  
      Compatible with both ctc_decoding or TLG decoding.
      """
      assert (
          k2 is not None
      ), "k2 is not installed, please follow 'tools/installers' to install"
      paths = k2.random_paths(
          lats, num_paths=num_paths, use_double_scores=True
      )
      if isinstance(lats.aux_labels, torch.Tensor):
          word_seqs = k2.ragged.index(
              lats.aux_labels.contiguous(), paths
          )
      else:
          # '_k2.RaggedInt' object has no attribute 'contiguous'
          word_seqs = lats.aux_labels.index(paths)
          word_seqs = word_seqs.remove_axis(word_seqs.num_axes - 2)
  
      # With ctc_decoding, word_seqs stores token_ids.
      # With TLG decoding, word_seqs stores word_ids.
      word_seqs = word_seqs.remove_values_leq(0)
      unique_word_seqs, num_repeats, new2old = word_seqs.unique(
          need_num_repeats=True, need_new2old_indexes=True
      )
  
      seq_to_path_shape = unique_word_seqs.shape.get_layer(0)
      path_to_seq_map = seq_to_path_shape.row_ids(1)
      # used to split final computed tot_scores
      seq_to_path_splits = seq_to_path_shape.row_splits(1)
  
      unique_word_seqs = unique_word_seqs.remove_axis(0)
      word_fsas = k2.linear_fsa(unique_word_seqs)
  
      word_fsas_with_epsilon_loops = k2.add_epsilon_self_loops(
          word_fsas
      )
  
      am_scores, lm_scores = compute_am_scores_and_lm_scores(
          lats,
          word_fsas_with_epsilon_loops,
          path_to_seq_map,
          device,
          batch_size,
      )
  
      token_seqs = k2.ragged.index(lats.labels.contiguous(), paths)
      token_seqs = token_seqs.remove_axis(0)
  
      token_ids, _ = token_seqs.index(new2old, axis=0)
      token_ids = token_ids.tolist()
      # Now remove repeated tokens and 0s and -1s.
      token_ids = [
          remove_repeated_and_leq(tokens) for tokens in token_ids
      ]
      return (
          am_scores,
          lm_scores,
          token_ids,
          new2old,
          path_to_seq_map,
          seq_to_path_splits,
      )
  
imports: import math
  from typing import List, Tuple
  import torch
label_desc: Use image node_id 4 for calling a global function with example usage: nbest_am_lm_scores(lats, num_paths, device, batch_size) and returns: am_scores,  lm_scores,  token_ids,  new2old,  path_to_seq_map,  seq_to_path_splits