code: def extract_alignment(align_matrix, tgt_mask, src_len, n_best):
      """
      Extract a batched align_matrix into its src indice alignment lists,
      with tgt_mask to filter out invalid tgt position as EOS/PAD.
      BOS already excluded from tgt_mask in order to match prediction.
  
      Args:
          align_matrix (Tensor): ``(B, tgt_len, src_len)``,
              attention head normalized by Softmax(dim=-1)
          tgt_mask (BoolTensor): ``(B, tgt_len)``, True for EOS, PAD.
          src_len (LongTensor): ``(B,)``, containing valid src lengths
          n_best (int): a value indicating number of parallel translation.
          * B: denote flattened batch as B = batch_size * n_best.
  
      Returns:
          alignments (List[List[FloatTensor|None]]): ``(batch_size, n_best,)``,
           containing valid alignment matrix (or None if blank prediction)
           for each translation.
      """
      batch_size_n_best = align_matrix.size(0)
      assert batch_size_n_best % n_best == 0
  
      alignments = [[] for _ in range(batch_size_n_best // n_best)]
  
      # treat alignment matrix one by one as each have different lengths
      for i, (am_b, tgt_mask_b, src_len) in enumerate(
          zip(align_matrix, tgt_mask, src_len)
      ):
          valid_tgt = ~tgt_mask_b
          valid_tgt_len = valid_tgt.sum()
          if valid_tgt_len == 0:
              # No alignment if not exist valid tgt token
              valid_alignment = None
          else:
              # get valid alignment (sub-matrix from full paded aligment matrix)
              am_valid_tgt = am_b.masked_select(
                  valid_tgt.unsqueeze(-1)
              ).view(valid_tgt_len, -1)
              valid_alignment = am_valid_tgt[
                  :, :src_len
              ]  # only keep valid src
          alignments[i // n_best].append(valid_alignment)
  
      return alignments
  
file_path: OpenNMT-py/onmt/utils/alignment.py
imports: import torch
  from itertools import accumulate
  from onmt.constants import SubwordMarker
label_desc: Use image node_id 2 for calling a global function with example usage: extract_alignment(align_matrix, tgt_mask, src_len, n_best) and returns: alignments